Pergunta

I'm trying to build a wizard style site to help creating an object.
Each step user inputs some parameters. I want to create and persist the object in DB only when all parameters are submitted at the final step.

class ExperimentsController < ApplicationController
  def wizard_step_1
    render template: "experiments/wizards/step1"
  end

  def wizard_step_2
    render template: "experiments/wizards/step2"
  end

  def wizard_step_3
    binding.pry
    # how to access params in step 1 & 2 here?
  end
end

View step1 below:
<%= form_tag(action: 'wizard_step_2') do %>
    <%= text_field_tag("user_name") %>
    <%= submit_tag("Next >>") %>
<% end %>

View step2 below:
<%= form_tag(action: 'wizard_step_3') do %>
    <%= text_field_tag("user_email") %>
    <%= submit_tag("Next >>") %>
<% end %>

I didn't put routes info here as it's tested OK. When user visit step1 and input user name, when submitting form control goes to wizard_step_2 and show view step2. Here user inputs email and when clicking submit button execution goes to action wizard_step_3. Then my question is how to obtain user name and email the user inputs in previous steps?

I'd thought about gem Wicked, and Rails cache etc., and more or less they don't fulfill my needs. Is there a decent way doing this?

Foi útil?

Solução

I don't have huge experience with this, but you may be able to benefit from: Wizard Forms Railscast

I'd imagine this type of setup will save the step 1, step 2, etc params as session variables:

class ExperimentsController < ApplicationController
  def wizard_step_1
    render template: "experiments/wizards/step1"
  end

  def wizard_step_2
    render template: "experiments/wizards/step2"

    #Create session vars from step1 
    session[:user][:name] = params[:user_name]
  end

  def wizard_step_3
    binding.pry

    #Step2 is already passed as params ;)
    params[:user_name] = session[:user][:name]   
  end
end

View step1 below:
<%= form_tag(action: 'wizard_step_2') do %>
    <%= text_field_tag("user_name") %>
    <%= submit_tag("Next >>") %>
<% end %>

View step2 below:
<%= form_tag(action: 'wizard_step_3') do %>
    <%= text_field_tag("user_email") %>
    <%= submit_tag("Next >>") %>
<% end %>

I know this example is not very DRY, but it's the basis of what I'd start to look at. Essentially, you need to be able to persist data between instances of objects, which lends itself entirely to sessions

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top