Search code examples
ruby-on-railsrails-activerecordform-helpers

Why is nested form returning a compile error in Rails?


I have two models: newsavedmap and waypoints.

My newsavedmap:

    class Newsavedmap < ActiveRecord::Base
        has_many :waypoints, :dependent => :destroy
        accepts_nested_attributes_for :waypoints
    end

The form, which lives on maptry.html.erb

    <% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
<%= f.error_messages %>
<%= f.text_field :name, {:id=>"savemap_name", :size=>30, :value=>"New Map"}%></p>

     <%= f.fields_for :waypoints do |p| %>
    <%= p.select :waypoint, itinerary.locations_for_select,{},{ :multiple=>"true", :id=>"waypoints", :class=>"mobile-waypoints-remove"}%>   
    <% end %>
    <% end %>

For some reason, this is throwing the following error:

    ActionView::TemplateError (compile error .../maptry.html.erb:392: syntax error, unexpected ')'        
    ...ields_for :waypoints do |p| ).to_s); @output_buffer.concat ...

Thanks for any help you can offer!

FINAL WORKING VERSION FOR RAILS 2

    <% f.fields_for @waypoints do |p| %>
    <%= p.select :waypoint, {},{}, :multiple=>"true", :id=>"waypoints", :class=>"mobile-waypoints-remove" %>      
    <% end %>

Solution

  • You are ending you form_for before starting the nested form

    Try the below code

    <% form_for @newsavedmap, :html=>{:id=>'createaMap'} do |f| %>
      <%= f.error_messages %>
      <%= f.text_field :name, {:id=>"savemap_name", :size=>30, :value=>"New Map"}%></p>
    
    
    <%= f.fields_for :waypoints do |p| %>
        <%= p.select :waypoint, itinerary.locations_for_select,{},{ :multiple=>"true", :id=>"waypoints", :class=>"mobile-waypoints-remove"}%>   
    <% end %>
    <% end %>