Ruby on Rails : How to insert record with fetching value from database
There are 3 tables : Student, College and Opted_College
I have a page say, Select College page(Add Opted Colleges page).
A Student will log into the application , and opt for different colleges. This college list is fetched from "College" table. Once the college is selected , the details like : ( college_id + student_id )should be added in the Opted_College table. For instatnce the Opted_College value might look like :
id College_id student_id 1 2 4
2 - R V College (college name) 4 - TestStudent (Student name)
I have not added a field for Student ID in the Select College page. I need to capture the student ID from the session and this data needs to be added to Opted_College table.
But after the Select College page is successfully sub开发者_如何学Pythonmited, "NULL" value is entered for Student_ID column in the Opted_College table.
Please help.
In the create method of your OptedColleges controller you want something like this:
@optedcollege.student_id = current_user.id
@optedcollege.save
I am of course assuming you are using an authentication solution that provides a current_user helper method...
精彩评论