Ruby on rails 使用Wicked gem编辑记录

Ruby on rails 使用Wicked gem编辑记录,ruby-on-rails,Ruby On Rails,我必须使用多步骤表单来创建作业,为此我使用wicked gem 分配\u步骤\u控制器.rb class Teacher::AssignmentStepsController < ApplicationController include Wicked::Wizard steps :initial_information, :choose_questions, :order_questions, :confirmation def show ... rend

我必须使用多步骤表单来创建作业,为此我使用wicked gem

分配\u步骤\u控制器.rb

class Teacher::AssignmentStepsController < ApplicationController
  include Wicked::Wizard

  steps :initial_information, :choose_questions, :order_questions, :confirmation

  def show
    ...
    render_wizard
  end

  def update
    ...
    redirect_to next_wizard_path
  end
end
class Teacher::AssignmentsController < ApplicationController

  def new
    @assignment = current_teacher.assignments.new
    redirect_to teacher_assignment_steps_path(:initial_infomation)
  end

  def create
    render :new
  end
end
班主任::分配步骤控制器
分配控制器.rb

class Teacher::AssignmentStepsController < ApplicationController
  include Wicked::Wizard

  steps :initial_information, :choose_questions, :order_questions, :confirmation

  def show
    ...
    render_wizard
  end

  def update
    ...
    redirect_to next_wizard_path
  end
end
class Teacher::AssignmentsController < ApplicationController

  def new
    @assignment = current_teacher.assignments.new
    redirect_to teacher_assignment_steps_path(:initial_infomation)
  end

  def create
    render :new
  end
end
班主任::分配控制器

现在创建一个任务非常有效。但我的问题是,我如何使用相同的多步骤表单来编辑作业?

我必须为编辑链接创建一个新路由,看起来是这样的

resources :assignment_steps, only: [:show, :update] # to create
resources :assignments do
  resources :assignment_steps, only: [:show, :update] # to edit
end
class Teacher::AssignmentStepsController < ApplicationController
  include Wicked::Wizard

  steps :initial_information, :choose_questions, :order_questions, :confirmation

  def show
    case step
      when :initial_information
        @assignment = current_assignment
        build_option_group
        session[:assignment] = @assignment.attributes if session[:assignment]
      when :choose_questions
        @question_ids = session[:question_ids] if session[:question_ids]
      when :confirmation
        set_final_assignment
    end
    render_wizard
  end

  def update
    case step
      when :initial_information
        @assignment = current_assignment
        @assignment.attributes = assignment_params
        session[:assignment] = @assignment.attributes
        redirect_to next_wizard_path
      when :choose_questions
        session['question_ids'] = params[:question_ids]
        redirect_to next_wizard_path
      when :order_questions
        # set order of those questions.
        redirect_to next_wizard_path
      when :confirmation
        set_final_assignment
        flush_session
        # session[:assignment_id] = @assignment.id
        render_wizard(@assignment)
    end
  end

  def finish_wizard_path
    teacher_assignments_path
  end

  private
    def current_assignment
      params[:assignment_id] ? current_teacher.assignments.find(params[:assignment_id]) : current_teacher.assignments.new
    end

    def build_option_group
      group = []
      current_teacher.courses.includes(:sections).each do |c|
        group << [c.title, c.sections.collect { |s| [s.name, s.id]}]
      end
      @options = group
    end

    def assignment_params
      params.require(:assignment).permit(:title, :topics, :post_date, :section_id, :submit_date, :soft_submit)
    end

    def get_questions_from_session
      Question.find(session[:question_ids])
    end

    def set_final_assignment
      @assignment = current_assignment
      @assignment.attributes = session[:assignment]
      @assignment.questions = get_questions_from_session
    end

    def flush_session
      session[:assignment] = session[:question_ids] = nil
    end
end
现在我的控制器是这样的

resources :assignment_steps, only: [:show, :update] # to create
resources :assignments do
  resources :assignment_steps, only: [:show, :update] # to edit
end
class Teacher::AssignmentStepsController < ApplicationController
  include Wicked::Wizard

  steps :initial_information, :choose_questions, :order_questions, :confirmation

  def show
    case step
      when :initial_information
        @assignment = current_assignment
        build_option_group
        session[:assignment] = @assignment.attributes if session[:assignment]
      when :choose_questions
        @question_ids = session[:question_ids] if session[:question_ids]
      when :confirmation
        set_final_assignment
    end
    render_wizard
  end

  def update
    case step
      when :initial_information
        @assignment = current_assignment
        @assignment.attributes = assignment_params
        session[:assignment] = @assignment.attributes
        redirect_to next_wizard_path
      when :choose_questions
        session['question_ids'] = params[:question_ids]
        redirect_to next_wizard_path
      when :order_questions
        # set order of those questions.
        redirect_to next_wizard_path
      when :confirmation
        set_final_assignment
        flush_session
        # session[:assignment_id] = @assignment.id
        render_wizard(@assignment)
    end
  end

  def finish_wizard_path
    teacher_assignments_path
  end

  private
    def current_assignment
      params[:assignment_id] ? current_teacher.assignments.find(params[:assignment_id]) : current_teacher.assignments.new
    end

    def build_option_group
      group = []
      current_teacher.courses.includes(:sections).each do |c|
        group << [c.title, c.sections.collect { |s| [s.name, s.id]}]
      end
      @options = group
    end

    def assignment_params
      params.require(:assignment).permit(:title, :topics, :post_date, :section_id, :submit_date, :soft_submit)
    end

    def get_questions_from_session
      Question.find(session[:question_ids])
    end

    def set_final_assignment
      @assignment = current_assignment
      @assignment.attributes = session[:assignment]
      @assignment.questions = get_questions_from_session
    end

    def flush_session
      session[:assignment] = session[:question_ids] = nil
    end
end
班主任::分配步骤控制器组我必须为编辑链接创建一个新路由,看起来是这样的

resources :assignment_steps, only: [:show, :update] # to create
resources :assignments do
  resources :assignment_steps, only: [:show, :update] # to edit
end
class Teacher::AssignmentStepsController < ApplicationController
  include Wicked::Wizard

  steps :initial_information, :choose_questions, :order_questions, :confirmation

  def show
    case step
      when :initial_information
        @assignment = current_assignment
        build_option_group
        session[:assignment] = @assignment.attributes if session[:assignment]
      when :choose_questions
        @question_ids = session[:question_ids] if session[:question_ids]
      when :confirmation
        set_final_assignment
    end
    render_wizard
  end

  def update
    case step
      when :initial_information
        @assignment = current_assignment
        @assignment.attributes = assignment_params
        session[:assignment] = @assignment.attributes
        redirect_to next_wizard_path
      when :choose_questions
        session['question_ids'] = params[:question_ids]
        redirect_to next_wizard_path
      when :order_questions
        # set order of those questions.
        redirect_to next_wizard_path
      when :confirmation
        set_final_assignment
        flush_session
        # session[:assignment_id] = @assignment.id
        render_wizard(@assignment)
    end
  end

  def finish_wizard_path
    teacher_assignments_path
  end

  private
    def current_assignment
      params[:assignment_id] ? current_teacher.assignments.find(params[:assignment_id]) : current_teacher.assignments.new
    end

    def build_option_group
      group = []
      current_teacher.courses.includes(:sections).each do |c|
        group << [c.title, c.sections.collect { |s| [s.name, s.id]}]
      end
      @options = group
    end

    def assignment_params
      params.require(:assignment).permit(:title, :topics, :post_date, :section_id, :submit_date, :soft_submit)
    end

    def get_questions_from_session
      Question.find(session[:question_ids])
    end

    def set_final_assignment
      @assignment = current_assignment
      @assignment.attributes = session[:assignment]
      @assignment.questions = get_questions_from_session
    end

    def flush_session
      session[:assignment] = session[:question_ids] = nil
    end
end
现在我的控制器是这样的

resources :assignment_steps, only: [:show, :update] # to create
resources :assignments do
  resources :assignment_steps, only: [:show, :update] # to edit
end
class Teacher::AssignmentStepsController < ApplicationController
  include Wicked::Wizard

  steps :initial_information, :choose_questions, :order_questions, :confirmation

  def show
    case step
      when :initial_information
        @assignment = current_assignment
        build_option_group
        session[:assignment] = @assignment.attributes if session[:assignment]
      when :choose_questions
        @question_ids = session[:question_ids] if session[:question_ids]
      when :confirmation
        set_final_assignment
    end
    render_wizard
  end

  def update
    case step
      when :initial_information
        @assignment = current_assignment
        @assignment.attributes = assignment_params
        session[:assignment] = @assignment.attributes
        redirect_to next_wizard_path
      when :choose_questions
        session['question_ids'] = params[:question_ids]
        redirect_to next_wizard_path
      when :order_questions
        # set order of those questions.
        redirect_to next_wizard_path
      when :confirmation
        set_final_assignment
        flush_session
        # session[:assignment_id] = @assignment.id
        render_wizard(@assignment)
    end
  end

  def finish_wizard_path
    teacher_assignments_path
  end

  private
    def current_assignment
      params[:assignment_id] ? current_teacher.assignments.find(params[:assignment_id]) : current_teacher.assignments.new
    end

    def build_option_group
      group = []
      current_teacher.courses.includes(:sections).each do |c|
        group << [c.title, c.sections.collect { |s| [s.name, s.id]}]
      end
      @options = group
    end

    def assignment_params
      params.require(:assignment).permit(:title, :topics, :post_date, :section_id, :submit_date, :soft_submit)
    end

    def get_questions_from_session
      Question.find(session[:question_ids])
    end

    def set_final_assignment
      @assignment = current_assignment
      @assignment.attributes = session[:assignment]
      @assignment.questions = get_questions_from_session
    end

    def flush_session
      session[:assignment] = session[:question_ids] = nil
    end
end
班主任::分配步骤控制器