Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails Populating SELECT options from database

I feel like this is basic but can't find details anywhere.

I have a basic application created by generating scaffold.

There is form already built into this that enters data. I have a SELECT (drop down) box in the form. I would like to be able to have the OPTIONS in this select box be pulled from a database. Ideally, the end user would be able to add and edit these options.

I have no idea how to link that SELECT field in my form to where the OPTIONS will be STORED.

Can someone point me in the right direction as far as terminology as what to research? I'm coming up blank. I feel like this must be a common thing to do..

like image 682
Devin Avatar asked Feb 05 '13 21:02

Devin


1 Answers

As ByScripts' link includes, here is the script that worked for me (for those where time is not a luxury):

<%= collection_select(:lang_id, 0, Language.all, :id, :name) %>

Where Language is a table with one column called 'name' and an auto-assigned column id; :lang_id is the name of the element and 0 is the default selected index when the page loads.

like image 103
TekuConcept Avatar answered Oct 07 '22 02:10

TekuConcept