I'm using the cocoon gem for a nested attributes form, in this case Projectuser
has_many ContractLinks
. This all works fine, when I add a contract, it sends the form parameters as contract_links_attributes
:
Parameters: {"utf8"=>"✓", "authenticity_token"=>"[FILTERED]", "projectuser"=>{"project_id"=>"538", "user_id"=>"56", "contract_links_attributes"=>{"description"=>"test", "contract_link"=>"test"}, "commit"=>"Save"}
Now I want also have a Project
has_many ContractLinks
, so I changed the relationship to be polymorphic. This still works great for the Projectuser
, but not for the Project
. I reused the exact same form partial, but instead of sending the form paramters as contract_links_attributes
, it sends it as contract_links
. This results in an unpermitted paramater error of course. So why is the exact same form partial sending different parameter values?
This is the form for both Project
and Projectuser
:
= f.fields_for(:contract_links) do |contract_link|
= render "contract_link_fields", f: contract_link
And this is contract_link_fields
:
%tr.contract
%td= f.date_select :contract_date, include_blank: true, start_year: 10.year.ago.year
%td= f.text_field :description, class: "input-xlarge"
%td= f.text_field :contract_link, class: "input-xlarge"
This sounds like you are missing the accepts_nested_attributes_for :contract_links
in Project
.
The accepts_nested_attributes_for
method adds/defines the contract_links_attributes
method, and the fields_for
uses this method/index-key only if it is available.
Also see https://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html