ruby on rails 4 - Wicked Wizard _Totally_ 动态步骤顺序

ruby on rails 4 - Wicked Wizard _Totally_ dynamic step order

我几乎找到了我在上一个问题中需要的东西:Wicked Wizard dynamic step order

不幸的是,我仍在努力解决我的问题。我想让我的 Wicked Wizard 具有完全动态的步骤顺序(除了第一步)。每个响应都是属于每个问题数据库项的数据库项。我想使用所选响应中的 next_question 属性来确定下一步将进行。

class ExperimentsController < ApplicationController
def create
  @experiment = Experiment.new
  @experiment.save(validate: false)
  redirect_to experiment_steps_path(@experiment, Experiment.steps.first)
end
...
private
  def experiment_params
    params.require(:experiment).permit(:name, :questions, :responses, :form_step)
  end
end



class ExperimentStepsController < ApplicationController
include Wicked::Wizard
before_action :set_steps
before_action :setup_wizard

def show
  @experiment = Experiment.find(params[:experiment_id])
  @next_question = Response.find(params[:next_question])
  jump_to(:next_question)
  render_wizard
end

private

def set_steps
  self.steps = Question.pluck(:name)
end

def response_params
  params.fetch(:response, {}).permit(:next_question)
end

end

当我在 Rails 3.2 中使用 before_filter 尝试类似的策略时,它给了我一个错误,建议我改用 prepend_before_filter,这很有效。看起来 Rails 4: prepend_before_action 中存在类似的方法,因此它应该适合您。此外,我将元素转换为符号(而不是字符串),因此在您的情况下它会类似于 self.steps = Question.pluck(:name).map(&:to_sym)。希望对您有所帮助!