使用Wicked gem编辑记录

时间:2016-06-12 18:04:48

标签: ruby-on-rails

我必须使用多步形式来创建一个赋值,因为我使用了邪恶的宝石。

assignment_steps_controller.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

assignment_controller.rb

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

现在它非常适合创建作业。但我的问题是如何使用相同的多步形式编辑作业?

1 个答案:

答案 0 :(得分:4)

我必须为编辑链接创建一个新路径,它看起来像这样

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

现在一切都按预期工作:)。