3

I have a rails form with a nested form. I used Ryan Bates nested form with jquery tutorial and I have it working fine as far as adding the new fields dynamically.

But when I go to submit the form it does not save any of the associated attributes. However if the partial builds when the form loads it creates the attribute just fine. I can not figure out what is not being passed in the javascript that is failing to communicate that the form object needs to be saved.

Any help would be great.

class Itinerary < ActiveRecord::Base
  accepts_nested_attributes_for :trips
end

itinerary/new.html

<% form_for ([@move, @itinerary]), :html => {:class => "new_trip" } do |f| %>

  <%= f.error_messages %>
  <%= f.hidden_field :move_id, :value => @move.id %>
  <% f.fields_for :trips do |builder| %>
    <%= render "trip", :f => builder %>
  <% end %>
<%= link_to_add_fields "Add Another Leg to Your Trip", f, :trips %>

<p><%= f.submit "Submit" %></p>

<% end %>

application_helper.rb

 def link_to_remove_fields(name, f)
  f.hidden_field(:_destroy) + link_to_function(name, "remove_fields(this)")
 end

 def link_to_add_fields(name, f, association)
  new_object = f.object.class.reflect_on_association(association).klass.new
  fields = f.fields_for(association, new_object, :child_index => "new_#{association}") do |builder|
    render(association.to_s.singularize, :f => builder)
  end
  link_to_function(name, h("add_fields(this, \"#{association}\", \"#{escape_javascript(fields)}\")"))
 end

application.js

function add_fields(link, association, content) {
var new_id = new Date().getTime();
var regexp = new RegExp("new_" + association, "g")
$(link).parent().before(content.replace(regexp, new_id));
}
3
  • Everything seems to be very similar to the railscast example, the only thing I can see is that your model here doesn't have "has_many :trips", but you probably just forgot to add that to your question. Commented Apr 10, 2010 at 16:48
  • Thanks Corey, bad copy and paste, the association is def declared in my app. Can't seem to figure this out either. Commented Apr 10, 2010 at 19:36
  • It's been almost a year, so I doubt your still looking for a solution, but first step to debugging this is to look at your HTML for the newly added input, and check the params hash. If the HTML is wrong the hash will be wrong, and your model won't update. Commented Jan 30, 2011 at 0:42

3 Answers 3

4

Have you tried adding:

class Itinerary < ActiveRecord::Base
  attr_accessible :move_id, :trips_attributes
  accepts_nested_attributes_for :trips
end

Of course you would then need to add the fields for your Itinerary model also. Without knowing those I would be guessing what they are.

Sign up to request clarification or add additional context in comments.

1 Comment

The :trips_attributes in attr_accessible tip fixed a problem for me today, thanks!
3

I had exactly the same problem as you. I fixed it by removing the line

validates_associated :parent_model

from the nested model (in your case the Trip model).

Comments

0

I was having similar issue. Removing the attr_accessible line from parent model did the trick for me. I see that your Itenerary model is already devoid of that line, so this might not help you; but might help someone else.

1 Comment

Or you can add your nested attributes to your parent model like: attr_accessible :model_attributes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.