Multistep Nested Forms Using Rails
I'm trying to implement a multistep form using a nested form, but since I'm using a nested form, it's not working out like the railscast. I got this from railscast #217 (multistep form/wizards) http://railscasts.com/episodes/217-multistep-forms
old _event.html.erb form
<%= render "event_details", :f => f %>
<%= render "sandwich_details", :f => f %>
new version of _event.html.erb
<%= render "#{@event.current_step}_details", :f => f %>
I get a "undefined method current_step
for nil:NilClass" error.
I think this is because event
is nested inside user
, so I'm not directly
using a forms_for method like in the railscast. Here's the loop leading up to
this 开发者_如何转开发point. I followed the railscast up to the point where he refreshed the page
for the first time.
_forms.html.erb
<% form_for @user do |f| %>
.
.
.
<% f.fields_for :events do |event_form| %>
<%= render :partial => 'event', :locals => {:f => event_form } %>
<% end %>
<% end %>
event.rb
class Event < ActiveRecord::Base
belongs_to :user
attr_writer :current_step
def current_step
@current_step || steps.first
end
def steps
%w[event_details item_details]
end
How can I change "#{@event.current_step}_details"
to make it find current_step?
EDIT
It turns out I had to pass @event => :event
into the locals, and then add @event = Event.new
to the base model new
method in the controller.
Hi @reti notinh wrong with your methode but just a little chage you need is
class Event < ActiveRecord::Base
belongs_to :user
attr_writer :current_step
def current_step
@current_step || steps.first
end
def steps
%w[event item]
end
And rest is same as you did earlier, thums up.
精彩评论