Nathan
Nathan

Reputation: 53

ruby on rails 4 - Wicked Wizard _Totally_ dynamic step order

I've found almost what I needed in a previous question: Wicked Wizard dynamic step order

Unfortunately, I'm still struggling with my problem. I want to make my Wicked Wizard have totally dynamic step order (besides the first step). Each response is a database item belonging to each Question database item. I want to use the next_question attribute from the Response that is selected to determine which step will come next.

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

Upvotes: 3

Views: 880

Answers (1)

Onikoroshi
Onikoroshi

Reputation: 303

When I tried a similar strategy in Rails 3.2 with before_filter it gave me an error that suggested I use prepend_before_filter instead, which worked a treat. It looks like a similar method exists in Rails 4: prepend_before_action, so that should work for you. Also, I cast the elements as symbols (not strings), so in your case it would be something like self.steps = Question.pluck(:name).map(&:to_sym). Hope that helps!

Upvotes: 1

Related Questions