Rails: Nested Form for 2 Scaffolded Models?

Course Queries Syllabus Queries 2 years ago

0 3 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Syllabus Queries related to Course Queries. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (3)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 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" }
profilepic.png
manpreet 2 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

profilepic.png
manpreet 2 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.