Why does this select field appear multiple times in my Rails form?
I've got a Rails application that is using nested forms. Details follow
and I tried this solution (Rails 3 Nested Models unknown attribute Error),
but for some reason, the field is repeating multiple times instead of
listing and saving the options correctly. Thanks in advance for your help!
Model information for Newsavedmaps
has_many :waypoints, :dependent => :destroy
accepts_nested_attributes_for :waypoints
Newsavedmap_controller
def new
@newsavedmap = Newsavedmap.new
waypoint = @newsavedmap.waypoints.build
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @newsavedmap }
end
end
def edit
@newsavedmap = Newsavedmap.find(params[:id])
if @newsavedmap.itinerary.user_id == current_user.id
respond_to do |format|
format.html # edit.html.erb
format.xml { render :xml => @activity }
end
else
redirect_to '/'
end
end
Maptry View
<% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.error_messages %>
<% f.fields_for :waypoint do |w| %>
<%= w.select :waypointaddress, options_for_select(Waypoint.find(:all,
:conditions => {:newsavedmap_id => params[:newsavedmap_id]}).collect {|wp|
[wp.waypointaddress, wp.waypointaddress] }), {:include_blank => true},
{:multiple => true, :class => "mobile-waypoints-remove", :id
=>"waypoints"} %>
<% end %>
<% end %>
When I use the above code, my form works correctly, but submitting it
gives me this error:
UnknownAttributeError (unknown attribute: waypoint)
When I change ":waypoint do |w|" to ":waypoints do |w|" in the view, the
select field disappears when the user is creating a new record, and in the
edit view, the select field appears several times (however many waypoints
the user saved in the record.)
How can I get this form field to work properly?
No comments:
Post a Comment