Rails: Nested Form for 2 Scaffolded Models?

Course Queries Syllabus Queries 3 years ago

2.6K 2 0 0 0

User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 3 years ago

 

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"=>"

\r\n\tdad

\r\n"
} } }, "commit"=>"Create Syllabus" }
0 views
0 shares

profilepic.png
manpreet 3 years ago

My best guess is that creations in

current_user.creations.build(params[:syllabus]) 

refers to a collection of mission instead of syllabus. Its kindof hard to determine without the full error and user model.


0 views   0 shares

No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Similar Forum