So I have been working on a multi-step form in ruby on rails (1.9.3) using Active model, following both the railscast #217 and #219 and am having problems with routing to the next steps in my form from /awizard/new to /awizard/1 hopefully someone can help me out. (I cannot use a form gem and must write this myself) I think this is a routing type issue (and I’m not using the id I give the model but I cannot figure out where I should be using it) a little help would be much appreciated!
My controller code – (contollers/awizard_controller.rb)
class AwizardController < ApplicationController
def new
# New Asset Wizard
@wizard = Awizard.new(id: 1)
# Set session variable as initial step
session[:wizard_step] = @wizard.current_step
end
def update
@wizard = Awizard.new(id: 1) unless !@wizard.nil?
@wizard.current_step = session[:wizard_step] unless nil
if @wizard.valid?
if params[:back_button]
@wizard.previous_step
elsif @wizard.last_step?
@wizard.save if @wizard.all_valid?
else
@wizard.next_step
end
session[:wizard_step] = @wizard.current_step
end
if @wizard.changed?
render 'form'
else
@wizard.save
end
end
def show
render 'form'
end
end
My Model – (models/awizard.rb)
class AWizard
include ActiveModel::Validations
include ActiveModel::Conversion
include ActiveModel::Dirty
include ActiveModel::Naming
#This class is used to manage the wizard steps using ActiveModel (not ActiveRecord)
attr_accessor :id
attr_writer :current_step #used to write to current step
define_attribute_methods [:current_step] #used for marking change
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def current_step
@current_step || steps.first
end
def steps
%w[step1 step2 step3] #make list of steps (partials)
end
def next_step
current_step_will_change! #mark changed when moving stepped
self.current_step = steps[steps.index(current_step)+1] unless last_step?
end
def previous_step
current_step_will_change! #mark changed when moving stepped
self.current_step = steps[steps.index(current_step)-1] unless first_step?
end
def first_step?
current_step == steps.first
end
def last_step?
current_step == steps.last
end
def all_valid?
steps.all? do |step|
self.current_step = step
valid?
end
end
def step(val)
current_step_will_change!
self.current_step = steps[val]
end
def persisted?
self.id == 1
end
def save
#will do later
end
end
My views –
(/views/awizard/_form.html.erb)
<%= content_for :awizard_form do%>
<%= form_for(@wizard) do |f| %>
<%= render "#{@wizard.current_step}_step", :f => f %>
<%= f.submit "Previous", :name => "back_button" unless @wizard.first_step? %>
<%= f.submit "Continue", :name => "step" unless @wizard.last_step? %>
<% end %>
<% end %>
(/views/awizard/_step1.html.erb)
<div class="field">
<%= f.label 'Step1' %><br />
</div>
(/views/awizard/_step2.html.erb)
<div class="field">
<%= f.label 'Step2' %><br />
</div>
(/views/awizard/_step3.html.erb)
<div class="field">
<%= f.label 'Step3' %><br />
</div>
Routes code
resources :awizard
Error Message
The error message I get after clicking the first continue button is as so –
“Template is missing
Missing template awizard/form, application/form with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in: * “fakepath/app/views””
more in depth error log shown below –
Started PUT “/awizard/1” for 127.0.0.1 at 2013-01-07 10:12:06 +1300
Processing by AwizardController#update as HTML
Parameters: {“utf8″=>”✓”, “authenticity_token”=>””, “awizard”=>{“data”=>”data”}, “step”=>”Continue”, “id”=>”1”}
Completed 500 Internal Server Error in 1ms
ActionView::MissingTemplate (Missing template awizard/form, application/form with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :coffee]}. Searched in:
* “fakepath/app/views”
):
app/controllers/awizard_controller.rb:40:in `update’
Rendered fakepath/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/missing_template.erb within rescues/layout (0.6ms)
The error message states , that there is no partial with the name
formin theawizarddirectory. You should stick to the Rails convention about partials – a partial file is always prefixed by underscore .In your case try to rename
form.html.erbto_form.html.erb.EDIT : You can try to render your partial like this