How do I create a nested form for 2 scaffolded models?
I have a Syllabus model, which has many Missions. Previously, the only way to create Missions was through the Syllabus nested form (which I made with Railscasts). Yesterday, I decided that users should be able to create Missions by itself, so I scaffolded the Missions while skipping the migrations, so it created all the controllers and views for the existing Missions Model.
So here’s the problem: I can now create Missions by itself, but when I try to create Missions through the Syllabus nested form, it gives me an error “undefined method `difficulty’ for #<\Mission:0xb372ffc.>” I’m guessing it’s trying to pass in the Syllabus attributes to the Missions attribute, which isn’t defined in the Missions schema. (difficulty is an attribute only for Syllabus).
To sum up: Nested forms worked with “builder” when I only had Model for Missions, but when I scaffolded the Missions so it could exist by itself, I couldn’t use the nested form.
Because there were more than a couple files that may affect this, I created a gist.
https://gist.github.com/2631734
Btw, these are the params that should be passed in
{"utf8"=>"✓",
"authenticity_token"=>"YxkzNmIJl0jBA467tdHP2zqNv22DgKrzSx+WCFgwKtk=",
"syllabus"=>
{"category"=>"Technology and Internet",
"difficulty"=>"Beginner",
"title"=>"missions testing",
"description"=>"deccrip",
"missions_attributes"=>
{"0"=>
{"_destroy"=>"false",
"title"=>"plz",
"content"=>"<p>\r\n\tdad</p>\r\n"
}
}
},
"commit"=>"Create Syllabus"
}
I figured it out, but it is really messy… I’m sure there’s a better way to do it, but after struggling with this for a few days, I’m more than happy with this solution: