3

I am having trouble trying to update nested models in my form. I don't get any errors and but the attributes don't get updated.

I have the following model:

class Trip < ActiveRecord::Base
  has_many :segments
  accepts_nested_attributes_for :segments, allow_destroy: true
end

class Segment < ActiveRecord::Base  
  belongs_to :start_location, class_name: 'Location'
  belongs_to :end_location, class_name: 'Location'
  belongs_to :trip

  validates_presence_of :date, :start_location, :end_location  
end

class Location < ActiveRecord::Base
  has_many :segments
end

And have this code in the _form.html.erb:

<%= form_for @trip do |f| %>
...
  <%= f.fields_for :segments do |builder| %>
    <%= render 'segment_fields', f: builder %>
  <% end %>
...
<% end %>

And this in the partial _segment_fields.html.erb:

<%= f.collection_select :start_location_id, Location.order(:name), :id, :name %> -
<%= f.collection_select :end_location_id, Location.order(:name), :id, :name %> <br>
<%= f.date_field :date %>

In my controller I also permited the assigment of :segments_attributes

def trip_params
  params.require(:trip).permit(:name, :start_date, :end_date, :segments_attributes)
end

Does anybody know what I am lacking or doing wrong?

2
  • Can you post the server log generated while submitting the form? And also your trip_params should be params.require(:trip).permit(:name, :start_date, :end_date, segments_attributes: [:id,:start_location_id,:end_location_id,:date]) Commented Jul 5, 2014 at 18:38
  • It started working perfect with that change! Anyway the server log is: Parameters: {..., "trip"=>{"name"=>"Sudeste2", "start_date"=>"2014-07-01", "end_date"=>"2014-07-31", "segments_attributes"=>{"0"=>{"start_location_id"=>"1", "end_location_id"=>"8", "date"=>"2014-06-30", "id"=>"2"}}}, "commit"=>"Update Trip", "id"=>"1"} Commented Jul 5, 2014 at 19:09

1 Answer 1

2

When you are creating a new record you don't need its id to be permitted as it's not been created but when you want to update your record you need to pass id to the permitted attributes, else it will work with create but not when you want to update your record, so you need to do:

def trip_params
  params.require(:trip).permit(:id, :name, :start_date, :end_date,  segments_attributes: [:id,:start_location_id,:end_location_id, :date])
end
Sign up to request clarification or add additional context in comments.

2 Comments

I have a question about setting the :id in strong parameters. Why? I'm very confused as to how that id is at all associating itself because it doesn't really match the DB id. Also, I do notice that it says Unpermitted parameter: id. So I definitely believe you. But this isn't documented very well.
@Andy The id attribute in segments_attributes is not the Trip's id but the Segment's id. Remember: they are two different records in the database here. A Trip and a Segment. They have one id each.

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.