polymorphism - Rails 4 : has_many through association on an belongs_to polymorphic association -
I try to do is_many through an association on an as_to_to polymorphic association through the union, I created for it, class 3:
Opinion RB:
class opinion & lt; ActiveRecord :: Base has_many: opinion_types has_many: through name_applications,: opinion_types, source: typeapplication, SOURCE_TYPE: "NameApplication" accepts_nested_attributes_for: opinion_types end
opinion_type.rb:
< Previous>name_application.rb:
class NameApplication & LT; ActiveRecord :: Base has_one: as opinion_type,: typeapplication has_one: through the opinion,: opinion_type end
My OpinionsController:
class OpinionsController & LT; ApplicationController def new @ opinion.new 3.times do @ opinion.name_applications & lt; & Lt; NameApplication.new end end def make @opinion = Opinion.new (parameter [opinions_params]) @ opinion.save redirect_to root_path end private def opinions_params params.require (: opinion) .permit (opinion_types_attributes: [name_applications: [: name]]) End of End
My new.html.erb
& lt;% = form_for @opinion do | O | & Gt%; & Lt;% = o.fields_for: opinion_types do | Ot | & Gt%; For & lt;% = ot.fields_: name_applications do | Na | & Gt%; & Lt;% = na.label: Name, "Name"%> & Lt; Br / & gt; & Lt;% = na.text_field: name% & gt; & Lt; Br / & gt; & Lt;% end% & gt; & Lt;% end% & gt; & Lt;% = o.submit% & gt; & Lt;% end% & gt; After submitting the form with 3 names, I get:
{"utf8" => gt; "✓", "Authenticity_token" = & gt; "Blablabla", "opinion" => {"Opinion_types_attributes" = & gt; {"0" = & gt; {"Name_applications" = & gt; {"Name" = & gt; "name test"}}, "1" = & gt; {"Name" = "}," commit "=>
The problem is: I have an error
unknown attribute: name_applications
When I write in Rail Console:
a.php.new a.name_applications & lt; NameApplication.new (name: "test" ) A.name_applications & lt; NameApplication.new (name: "test2") a.name_applications & lt; name & nbsp; application. New (name: "test3") a.save
My opinion model and my name app models are related to my opinion model which saves properly.
I Used different positions to try not to find, but without success:
I do not know that my the problem is:
- my
accepts_nested_attributes_for: typeapplication
my opinion_type model (because it is a polymorphic association)- My strong parameters:
params .require (: opinion) .Permit (opinion_ types_attributes: [name_applications: [: name]])
- or other
Thank you for your help
Update: I've got a solution, but not very clean In it:
controller in my opinion I have changed my composition by
def make @ Opinion = Opinianknveen Ray_prm [: Ray_taip_aditrs] Kiiiisi | F, Index | @ Opinion.name_applications & lt; & Lt; Name, application.new (ray_param [: rai_type_antivs] [: "# {index}"] [: name_applicix]) @ opinion save_directory root_path end
I still do not understand Why can I?
I am grateful for any proposal that can help me understand.
P>
Comments
Post a Comment