# Selects and the database

**URL:** <https://www.ruby-forum.com/t/selects-and-the-database/111549>\
**Category:** Rails\
**Created:** [August 8, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/selects-and-the-database/111549 "2007-08-08T15:25:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 8, 2007, 3:25pm UTC](https://www.ruby-forum.com/t/selects-and-the-database/111549/1 "2007-08-08T15:25:21Z")

</div>

I have this select drop down menu that I would like to have either show  
default values for the menu or have it grab from the database the values  
already selected (as the user can change their selections anytime they  
want). I imagine that I would have to be using an id somewhere, but Im  
just not sure on where to even start. Here is my code for the select:

\<%= select ‘airlineinfo’, ‘smoking’, [[‘Non-Smoking’], [‘Smoking’] ] %\>

Pretty straight forward. airlineinfo is the name of the table in the  
database and smoking is the name. Thanks,

~S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 8, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/selects-and-the-database/111549/2 "2007-08-08T16:01:59Z")

</div>

Shandy N. wrote:

> I have this select drop down menu that I would like to have either show  
> default values for the menu or have it grab from the database the values  
> already selected (as the user can change their selections anytime they  
> want). I imagine that I would have to be using an id somewhere, but Im  
> just not sure on where to even start. Here is my code for the select:
> 
> \<%= select ‘airlineinfo’, ‘smoking’, [[‘Non-Smoking’], [‘Smoking’] ] %\>
> 
> Pretty straight forward. airlineinfo is the name of the table in the  
> database and smoking is the name. Thanks,
> 
> ~S

what are the values in the db? is this a boolean field ( smoking → true  
/ false ) or a text field? if it is a simple yes / no smoking option  
(boolean) you could use a checkbox rather than a select … if you need  
a select box, the way to go is using the below;

(select\_tag:) #creates the html tag  
[http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M000604](http://api.rubyonrails.com/classes/ActionView/Helpers/FormTagHelper.html#M000604)

(options\_from\_collection\_for\_select:) #creates the html options tags  
[http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000511](http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000511)

OR

(options\_for\_select:)  
[http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000510](http://api.rubyonrails.com/classes/ActionView/Helpers/FormOptionsHelper.html#M000510)

========in the end u’d do something like:============

select\_tag ‘airlineinfo[smoking]’,  
options\_from\_collection\_for\_select(@fliers, ‘id’, ‘flier\_name’)

or whatever suits your needs (which i haven’t really understood)  
anyway… hth  
…

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 8, 2007, 4:42pm UTC](https://www.ruby-forum.com/t/selects-and-the-database/111549/3 "2007-08-08T16:42:15Z")

</div>

I guess the idea that I’m trying to convey is that a user can go and  
fill out their preferences for seating, smoking, etc., in a form. Later,  
if they so choose too, they can edit or change the values that they have  
made in that form, switch their window seat preferences to aisle, for  
example. What I would like to happen is for their original preferences  
to be visable every time they go back to modify the form (or any changes  
that are made). This way, to me, it just re-assures the user that the  
values they entered in the original filling out of the form took and  
were saved. The way the form is set-up now, however, is to show the  
default values in the select boxes whenever the user goes back to visit  
that form.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 8, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/selects-and-the-database/111549/4 "2007-08-08T16:44:35Z")

</div>

This sounds like an html question! I haven’t tried this, frankly, but  
seeing how most of the options that goes under helper class methods  
seem to be html options themselves, you might try this:

\<%= select\_tag ‘airlineinfo’, ‘smoking’, [[‘Non-Smoking’],  
[‘Smoking’] ], :selected=\>‘Smoking’ %\>

Or even better:

\<%= select\_tag ‘airlineinfo’, ‘smoking’, [[‘Non-Smoking’],  
[‘Smoking’] ], :selected=\>airlineinfo.smoking.to\_s %\>

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 8, 2007, 6:07pm UTC](https://www.ruby-forum.com/t/selects-and-the-database/111549/5 "2007-08-08T18:07:02Z")

</div>

Whoa, it actually worked!? COOL :-)! I only guessed.

On Aug 8, 11:10 am, Shandy N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [August 8, 2007, 5:10pm UTC](https://www.ruby-forum.com/t/selects-and-the-database/111549/6 "2007-08-08T17:10:05Z")

</div>

Yes, thank you again, that works perfectly.
