Welcome to the Treehouse Community
Looking to learn something new?
Help needed on rails progress app
I need help on making an app in which a teacher logs in and has a list of students, where he can add/edit/delete a student. When a student's name is clicked on, the teacher will then be redirected to a page where there are static stories in which the teacher can check or uncheck depending on student completion (mind you, the teacher cannot edit the stories, hence the static).
In conclusion, I would like to figure out 3 things:
- I would like to know the controllers/models/associations that are needed to make this work
- How someone could go about having the stories automatically created when a new student is created
- Obviously having the checkboxes save so when the teacher logs out, the changes are remembered
I have looked everywhere and still did not find a tutorial that goes through any of these topics in detail (eg. checkboxes).
I do not necessarily need an answer to all of these questions at once, but any help would be greatly appreciated
Brandon Barrette20,485 Points
You are quite close. You then need another join model called StudentStory that contains the student_id and the story_id. This is where I would but the "done" button. Then your associations would be:
class Story has_many :student_stories has_many :students, through: :student_stories end class StudentStory belongs_to :student belongs_to :story end class Student has_many :student_stories has_many :stories, through: :student_stories end
You checkbox solution is then fixed because each checkbox would correspond to a row in the StudentStory table. If it exists, then the checkbox is checked, otherwise it is not. You would use an accepts_nested_attributes_for: :student_stories to achieve that.
And to answer your second question, you don't need to worry about it. The stories are independent of the students, so you can have all the stories show up for each student, then use checkboxes to select the ones you want.
Brandon Barrette20,485 Points
You would need to actually generate a new model using the rails generate command
rails generate model student_story student:references story:references
Then you wouldn't need a controller for this, you'd just need to add accepts nested attributes for under the student model. This is some good video about nested attributes (totally worth the $9)
I'd create two forms on the student model, one to make new students (the standard) and one to add stories to students. This is where the join model would appear and going to a link say:
would bring up the form with all the checkboxes for that student.
If you have other general questions, I'd be glad to help, but that's about as far as I can go. I'd say start coding something up and you can always ask more specific questions in the forum if you get stuck. The only way to really learn is try, fail many times, and learn from those failures. I sure have done it and still do it everyday. I'm always learning new stuff. Good luck!