Skywalker
2008-Feb-28 09:54 UTC
Foreign keys not displayed for user input in create function
Newbie to Ruby - would appreciate it enormously if somebody could help me with this. In my create function, for a specific table, my foreign keys in that table are not visible for selection. However, at the time of writing to the database, they take one or the other values on these foreing keys. I carried out the validates_presence_of on these foreign keys and that started generating errors but it still did not show the foreign keys. Example Exam |---------< Exam_Subjects>--------| SubjectsExam_id Exam_id (foreign- key) Subject_id Exam_Name Subject_id (foreign- key) Subject_Name Percent_of_questions Difficulty_level_of_subjects After generating the scaffolding, when I go to exam_subject/create screen, it only displays text-boxes to input Percent_of_questions and Difficulty_level_of_subjects. Its doesnt show Subject_id or Exam_id for input and takes default values to fill it. What I need is drop downs for Exam_Name and Subject_Name. I tried using the validates_presence_of Exam_id which actually resulted in generating an error but still no way of beinbg able to show the name fields for input. I was going to write a code but think there must be some ruby method I can use to show all foreign keys for input? Is there one? Am I missing something? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jabbslad
2008-Feb-28 12:19 UTC
Re: Foreign keys not displayed for user input in create function
Hi Sky, You need to add something like this to your form assuming you are using form_for:- <%= f.select(''exam_id'', @exams.collect {|exam| [exam.name, exam.id ]}) %> You''ll initialise exams instance variable in your controller (@exams Exam.find(:all)). You need to do something similar for the the Subject drop down. I hope that answers your question? Jabbslad On Feb 28, 9:54 am, Skywalker <vikram.b...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Newbie to Ruby - would appreciate it enormously if somebody could help > me with this. > > In my create function, for a specific table, my foreign keys in that > table are not visible for selection. However, at the time of writing > to the database, they take one or the other values on these foreing > keys. I carried out the validates_presence_of on these foreign keys > and that started generating errors but it still did not show the > foreign keys. > > Example > > Exam |---------< Exam_Subjects>--------| Subjects > > Exam_id Exam_id (foreign- > key) Subject_id > Exam_Name Subject_id (foreign- > key) Subject_Name > Percent_of_questions > > Difficulty_level_of_subjects > > After generating the scaffolding, when I go to exam_subject/create > screen, it only displays text-boxes to input Percent_of_questions and > Difficulty_level_of_subjects. Its doesnt show Subject_id or Exam_id > for input and takes default values to fill it. > > What I need is drop downs for Exam_Name and Subject_Name. > > I tried using the validates_presence_of Exam_id which actually > resulted in generating an error but still no way of beinbg able to > show the name fields for input. > > I was going to write a code but think there must be some ruby method I > can use to show all foreign keys for input? Is there one? Am I missing > something?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---