# Selectboxes and Rails

**URL:** <https://www.ruby-forum.com/t/selectboxes-and-rails/62012>\
**Category:** Rails\
**Created:** [May 30, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/selectboxes-and-rails/62012 "2006-05-30T20:05:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ray\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Ray\_M](https://www.ruby-forum.com/u/Ray_M)\
**Post date:** [May 30, 2006, 8:05pm UTC](https://www.ruby-forum.com/t/selectboxes-and-rails/62012/1 "2006-05-30T20:05:10Z")

</div>

Hey was wondering how to setup a select box that when I call  
Model.new(params[:model]) it will automatically putit into the db…

I tried  
  
0  
…

then when I do @home = Home.new(params[:home]) that field doesnt insert  
into the db… any ideas?

-Ray

---

<div class="post-metadata">

**Author:** ![Ray\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Ray\_M](https://www.ruby-forum.com/u/Ray_M)\
**Post date:** [May 30, 2006, 8:15pm UTC](https://www.ruby-forum.com/t/selectboxes-and-rails/62012/2 "2006-05-30T20:15:19Z")

</div>

Did you remember to execute @home.save afterward?

Ray M. wrote:

> Hey was wondering how to setup a select box that when I call  
> Model.new(params[:model]) it will automatically putit into the db…
> 
> I tried
> 
> 0 ...
> 
> then when I do @home = Home.new(params[:home]) that field doesnt insert  
> into the db… any ideas?
> 
> -Ray

---

<div class="post-metadata">

**Author:** ![Ray\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Ray\_M](https://www.ruby-forum.com/u/Ray_M)\
**Post date:** [May 30, 2006, 8:31pm UTC](https://www.ruby-forum.com/t/selectboxes-and-rails/62012/3 "2006-05-30T20:31:43Z")

</div>

Steve K. wrote:

> Did you remember to execute @home.save afterward?
> 
> Ray M. wrote:
> 
> > Hey was wondering how to setup a select box that when I call  
> > Model.new(params[:model]) it will automatically putit into the db…
> > 
> > I tried
> > 
> > 0 ...
> > 
> > then when I do @home = Home.new(params[:home]) that field doesnt insert  
> > into the db… any ideas?
> > 
> > -Ray

Well it saves all the other information into the database, except for  
that one field… the select field.

---

<div class="post-metadata">

**Author:** ![Ray\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Ray\_M](https://www.ruby-forum.com/u/Ray_M)\
**Post date:** [May 30, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/selectboxes-and-rails/62012/4 "2006-05-30T20:42:09Z")

</div>

On 5/30/06, Ray M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > ```
> > > <option value="0">0</option>
> > > 
> > > ```

Try changing it to:  
  
And see the magic happen…

–

---

<div class="post-metadata">

**Author:** ![Ray\_M](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@Ray\_M](https://www.ruby-forum.com/u/Ray_M)\
**Post date:** [May 30, 2006, 8:54pm UTC](https://www.ruby-forum.com/t/selectboxes-and-rails/62012/5 "2006-05-30T20:54:35Z")

</div>

Hey Ray,

if the Home attribute is “bedrooms”, as in Home.new().bedrooms, _and_  
you are getting params[:home] then the name attribute of your select  
tag should be “home[bedrooms]”.

The rails form helpers will keep you on the right track here:

[http://api.rubyonrails.com/classes/ActionView/Helpers/](http://api.rubyonrails.com/classes/ActionView/Helpers/)  
FormOptionsHelper.html#M000399

as in \<%= select :home, :bedrooms, [0,1,2,3,4,5] %\>

will create a select box with a name of home[bedrooms] with an option  
list from 0 through 5. Although I’ve not tried it you could probably  
use a ruby “Range” of (0…5) as well.

## Regards, Trevor

Trevor S.  
[http://somethinglearned.com](http://somethinglearned.com)
