Hi, I''m a newbie on both Ruby and Ruby on Rails, and I''m struggling with my first app. I have a table with a column called "employee_id" which is a foreing key associating it with the "employees" table. I have populated both tables. When I run "ruby script/generate scaffold <model name> <controller>" it generates a web page with all fields except the foreign key field. I had hoped it would make i.e. a drop down with data from the employees table. How do I do this? I''m using MySQL 5.0 on WindowsXP. TIA, Jakob
Jakob Rohde wrote:> Hi, > > I''m a newbie on both Ruby and Ruby on Rails, and I''m struggling with > my first app. I have a table with a column called "employee_id" which > is a foreing key associating it with the "employees" table. I have > populated both tables. > > When I run "ruby script/generate scaffold <model name> <controller>" > it generates a web page with all fields except the foreign key field. > I had hoped it would make i.e. a drop down with data from the > employees table. > > How do I do this? > > I''m using MySQL 5.0 on WindowsXP. > > TIA, > > JakobYou need to create an option form field for your view. Using the rails helper method of options_from_collection_for_select. In your partial template, you''re going to put: <%= options_from_collection_for_select @employees, "id", "name", @project.employee_id %> Just remember you''re going to have to define @employees in your project controller with something like @employees = Employee.find(:all) -- Posted via http://www.ruby-forum.com/.
In your view _form.rhtml <select id="primary_table_employee_id" name="primary_table[employee_id]"> <%= options_from_collection_for_select(Employees.find(:all), "id", "firstname", @primary_table.employee_id ? i @primary_table.employee_id : -1) %> </select> On 12/22/05, Jakob Rohde <jakob.rohde-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi, > > I''m a newbie on both Ruby and Ruby on Rails, and I''m struggling with > my first app. I have a table with a column called "employee_id" which > is a foreing key associating it with the "employees" table. I have > populated both tables. > > When I run "ruby script/generate scaffold <model name> <controller>" > it generates a web page with all fields except the foreign key field. > I had hoped it would make i.e. a drop down with data from the > employees table. > > How do I do this? > > I''m using MySQL 5.0 on WindowsXP. > > TIA, > > Jakob > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- First they laugh at you, then they ignore you, then they fight you. Then you win. -- Mahatma Karamchand Gandhi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hi, Isn''t there something wrong in the second line of this snippet? Jakob -----Oprindelig meddelelse----- Fra: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] På vegne af Leon Leslie Sendt: 22. december 2005 22:59 Til: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Emne: Re: [Rails] Ruby on Rails and lookup tables In your view _form.rhtml <select id="primary_table_employee_id" name="primary_table[employee_id]"> <%= options_from_collection_for_select(Employees.find(:all), "id", "firstname", @primary_table.employee_id ? i @primary_table.employee_id : -1) %> </select> On 12/22/05, Jakob Rohde <jakob.rohde-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: Hi, I''m a newbie on both Ruby and Ruby on Rails, and I''m struggling with my first app. I have a table with a column called "employee_id" which is a foreing key associating it with the "employees" table. I have populated both tables. When I run "ruby script/generate scaffold <model name> <controller>" it generates a web page with all fields except the foreign key field. I had hoped it would make i.e. a drop down with data from the employees table. How do I do this? I''m using MySQL 5.0 on WindowsXP. TIA, Jakob _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails -- First they laugh at you, then they ignore you, then they fight you. Then you win. -- Mahatma Karamchand Gandhi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On 12/22/05, Marc Love <marc.love-w5+zYyZo3JRBDgjK7y7TUQ@public.gmane.org> wrote:> You need to create an option form field for your view. Using the rails > helper method of options_from_collection_for_select. > > In your partial template, you''re going to put: > > <%= options_from_collection_for_select @employees, "id", "name", > @project.employee_id %> > > Just remember you''re going to have to define @employees in your project > controller with something like @employees = Employee.find(:all) >Butting in as anothoer newbie: I tried to follow the above advice for a pull-down of my own. I''m trying to select/populate my case_type_id field. On my edit form I now have a populated pull down that reflects my case_type table. It also initially has default selection that is derived from the database. So I have a lot of functionality. What doesn''t work is modifying the actual database filed. ie. my case_type_id field in the database is not being updated based on the pulldown. Any ideas? My code looks like: <select id="case_case_type_id" name="Case[case_type_id]"> <%= options_from_collection_for_select @case_types, "id", "case_type", @case.case_type_id %> </select> I have not added any code to extract the selection, but I assume that is somehow automatic? Thanks Greg -- Greg Freemyer The Norcross Group Forensics for the 21st Century
Greg Freemyer wrote:> What doesn''t work is modifying the actual database filed. ie. my > case_type_id field in the database is not being updated based on the > pulldown. > > Any ideas? My code looks like: > <select id="case_case_type_id" name="Case[case_type_id]"> > <%= options_from_collection_for_select @case_types, "id", "case_type", > @case.case_type_id %> > </select> > > I have not added any code to extract the selection, but I assume that > is somehow automatic? > > Thanks > GregYes, it is automatic. The problem lies somewhere in the name for your select tag. My initial guess is that its because you''re capitalizing "Case". It should read "case[case_type_id]". Also, just double checking, but is the row in your table actually called "case_type_id" or is it just "type_id"? What''s in the brackets should match exactly what your row is called. -- Posted via http://www.ruby-forum.com/.
On 12/23/05, Marc Love <marc.love-w5+zYyZo3JRBDgjK7y7TUQ@public.gmane.org> wrote:> Greg Freemyer wrote: > > What doesn''t work is modifying the actual database filed. ie. my > > case_type_id field in the database is not being updated based on the > > pulldown. > > > > Any ideas? My code looks like: > > <select id="case_case_type_id" name="Case[case_type_id]"> > > <%= options_from_collection_for_select @case_types, "id", "case_type", > > @case.case_type_id %> > > </select> > > > > I have not added any code to extract the selection, but I assume that > > is somehow automatic? > > > > Thanks > > Greg > > Yes, it is automatic. The problem lies somewhere in the name for your > select tag. My initial guess is that its because you''re capitalizing > "Case". It should read "case[case_type_id]". Also, just double > checking, but is the row in your table actually called "case_type_id" or > is it just "type_id"? What''s in the brackets should match exactly what > your row is called. >Thanks, not sure how it was working but monkeying with the above and changing some variable names I got an error that my list was not being declared in the update method. Doing so caused it all to work. Thanks Greg -- Greg Freemyer The Norcross Group Forensics for the 21st Century
I have a problem with very similar issue: I have ''employees'' and ''assignments'' table. In ''assignments'' table I have an ''employee_id'' field. There are separate fields for first name and last name in the ''employees'' table. So when adding an assignment I select an employee from drop down list. In the ''options_from_collection_for_select'' method I specify the field to be displayed on the list (first name or last name): <%= options_from_collection_for_select @employees, :id, "last_name", @assignment.employee_id %> But is it possible to somehow concatenate the first name and last name fields so the full name of each employee is displayed on the list? Doing something like: <%= options_from_collection_for_select @employees, :id, "first_name" + "last_name", @assignment.employee_id %> didn''t work. Do You have any solution? Greetings, Quintus -- Posted via http://www.ruby-forum.com/.