I am trying to make a nested form that can be edited then saved. Whenever I go to save the form though I get the error:
Couldn't find ClassInstance with ID=11 for Collection with ID=
11 is the ClassInstance id for the first ClassInstance in the form, and it appears that the Collection id is being set to NULL.
I have tried to manually set hidden ids in the controller form_for
as well as setting a hidden field in _class_instance_fields.html.erb for the :collection_id.
What is ActiveRecord trying to do and why is it getting confused? Am I doing something you aren't supposed to with nested forms?
I have included some of my code with the fluff removed so that it isn't as much to look through.
collections_controller.rb
def update
@collection = Collection.new(params[:collection])
if @collection.save
flash[:notice] = "IT WORKED"
else
flash[:notice] = "No Cigar...."
end
redirect_to collection_path
end
def read
@collection = Collection.find_by_user_id(current_user.id)
@classes = Classification.all
end
read.html.erb
<%= form_for @collection, url: {:action => "update", collection_id: @collection.id} do |f| %>
<%= f.hidden_field :id #I had hoped this would fix it%>
<table class="instance_table">
<%= f.fields_for :class_instances do |builder| %>
<%= render 'shared/class_instance_fields', :f => builder, :status => '0' %>
<% end %>
<tr><td>
<%= link_to_add_fields "Add an Instance", f, :class_instances %>
</td></tr>
</table>
<%= f.submit "Save"%>
<% end %>
_class_instance_fields.html.erb
<tr class="record_row">
<td>
<%= f.label( :name) %></p>
</td>
<%= f.hidden_field :status, :value => status %>
<%= f.hidden_field :collection_id %>
<td>
<% if status == '1' %>
<%= f.text_field :name %>
<% else %>
<%= link_to f.object.name, instance_edit_path(f.object.id) %>
<% end %>
</td>
<td>
<% if status == '1' %>
<%= f.select :classification, options_for_select(@classes.collect{|c| [c.name, c.id]},selected: f.object.class_id)%>
<% else %>
<%= f.label :classification, displayClassInstanceName(@classes.select{|a| a[:id] == f.object.class_id }[0]) %>
<% end %>
</td>
<td>
<% user = nil%>
<% id = nil %>
<% if status == '1' %>
<%collection = current_user.id %>
<% else %>
<%collection = f.object.collection_id %>
<% end %>
<%= f.label :username, displayUserName( user ) %>
</td>
<td>
<%= removeRecordLink(f) %>
</td>
</tr>
Additional Info
I have been using Rails.logger.debug
to inspect the params before I save them. With the folling code in the `update
Rails.logger.debug "TG: " << params.to_yaml
I get the following output
TG: --- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
_method: put
authenticity_token: efvqGsLKJfdCsadfjsad9fsD97dfgYaOUjsg+uvAQi2+k4=
collection: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
name: mikee's Collection
class_instances_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
'0': !ruby/hash:ActiveSupport::HashWithIndifferentAccess
status: '0'
collection_id: '8'
_destroy: '0'
instance_attributes_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
'0': !ruby/hash:ActiveSupport::HashWithIndifferentAccess
id: '1'
_destroy: '0'
id: '11'
commit: Save
controller: collections
action: update
id: '8'
Maybe that can help make sense of things, as I am not too experience with params and I'm not sure what I should be seeing there for nested forms.