Rails grouped_collection_select not working when saving
Asked Answered
B

1

7

I'm using grouped_collection_select in filtering out associated information in a rails 5 form.

The first grouped_collection_select works with the Property filtering out associated data relevant to Co-operators. But, the second grouped_collection_select does work when filtering Fields associated to a Property, but comes up with an error when trying to save:

1 error prohibited this trial from being saved:

Field must exist

Form

<%= form_with(model: trial, local: true) do |f| %>
 <label>Co-operator</label>
 <%= f.collection_select :cooperator_id, Cooperator.order('last_name'), :id, :full_name %>
 <label>Property</label>
 <%= f.grouped_collection_select :property_id, Cooperator.order('last_name'), :properties, :full_name, :cooperator_id, :name %>
 <label>Field</label>
 <%= f.grouped_collection_select :field_id, Property.order('name'), :fields, :name, :property_id, :field_name %>
 <%= f.submit 'Submit' %>
<% end %>

When I change the grouped_collection_select to a collection_select it works as should. But, this does't suit what i'm needing.

<%= f.collection_select :field_id, Field.all, :id, :field_name %>

Trials Controller

def trial_params
 params.require(:trial).permit(:cooperator_id, :field_id, :property_id)
end

Trial Model

class Trial < ApplicationRecord
  belongs_to :cooperator
  belongs_to :property
  belongs_to :field
end

Log

Processing by TrialsController#update as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"THfy+JGBYbNvzurUscPfP8LQbnnvIz1HBEfeFRiZrocXtiu4ayncEA8cNBA2IkPgcphLoa0QWsEueFBEP29OXA==", "trial"=>{"cooperator_id"=>"2", "property_id"=>"2", "field_id"=>""}, "commit"=>"Create trial", "id"=>"11"}
Cooperator Load (0.5ms)  SELECT  "cooperators".* FROM "cooperators" WHERE "cooperators"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/trials_controller.rb:49
  Property Load (0.4ms)  SELECT  "properties".* FROM "properties" WHERE "properties"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
  ↳ app/controllers/trials_controller.rb:49
Field Load (0.4ms)  SELECT "fields".* FROM "fields"
  ↳ app/views/trials/_form.html.erb:39
Rendered trials/_form.html.erb (15.3ms)
  Rendered trials/edit.html.erb within layouts/application (16.6ms)
  Rendered partials/_top_nav.html.erb (0.5ms)
  Rendered partials/_main_nav.html.erb (0.8ms)
Completed 200 OK in 63ms (Views: 46.9ms | ActiveRecord: 8.2ms)
Biconvex answered 28/9, 2018 at 6:20 Comment(8)
why do you using this at the field control end ":field_name, :field_name %>" as you can see in control above you wrote ":cooperator_id, :name %> <label>Field</label>" so I suggest you to try on field control to change to ":field_id, :field_name %>"Kirkpatrick
also what are the fields for Cooperator and Property?Kirkpatrick
Sorry, that was a typo. Have updated it.Biconvex
Update the question with the Trial model with associations and also with relevant controller codeFixing
Also update the question with the params that appear in the server log when you submit the formFixing
Thanks @Fixing I have updated the question.Biconvex
Ok, the field_id is coming as nil. Can you inspect the HTML of the grouped_collection_select in the bowser's console and paste in the question?Fixing
I wonder why you have Property.order('name') for the collection in the grouped_collection_select and Field.all for the collection in the collection_select.Fixing
I
9

The form code doesn't look right to me, the first grouped collection should be something like:

<%= f.grouped_collection_select :property_id, Cooperator.order('last_name'), :properties, :full_name, :id, :name %> # Notice that the cooperator_id is replaced with id because this needs to be the value that should be set on selection. Your original code would set it to the ID of the Cooperator instead of Property.

Similarly, the second one should be something like:

<%= f.grouped_collection_select :field_id, Property.order('name'), :fields, :name, :id, :field_name %>

Impermeable answered 1/10, 2018 at 16:2 Comment(1)
Yep. That did it @chirag. Changing the :property_id to :id worked.Biconvex

© 2022 - 2024 — McMap. All rights reserved.