# Select boxe

**URL:** <https://www.ruby-forum.com/t/select-boxe/94208>\
**Category:** Rails\
**Created:** [April 25, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/select-boxe/94208 "2006-04-25T16:17:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [April 25, 2006, 4:17pm UTC](https://www.ruby-forum.com/t/select-boxe/94208/1 "2006-04-25T16:17:03Z")

</div>

yop,

[http://rubyonrails.org/api/classes/ActionView/Helpers/FormHelper.html](http://rubyonrails.org/api/classes/ActionView/Helpers/FormHelper.html)

ici j’ai vu qu’on pouvait gÃ©nÃ©rer des Ã©lÃ©ment d’un formulaire grÃ¢ce des  
mÃ©thodes. Apres quelque petite recherches j’ai trouvÃ© comment gÃ©nÃ©rer un  
select

\<%= select(‘category’, ‘category.id’, Category.find\_all.collect  
{|category|  
category.nom}) %\>

comment faire pour utiliser l’option selected ?

Merci

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [April 25, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/select-boxe/94208/2 "2006-04-25T17:03:31Z")

</div>

oups sorry i wrote in French

i would like to know how to add a ‘selected option’ for selec box?

\<%= select(‘recipes’, ‘category.id’, Category.find\_all.collect {|c| [  
c.nom,  
c.id ] }) %\>

tks

2006/4/25, Bolo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [April 25, 2006, 5:22pm UTC](https://www.ruby-forum.com/t/select-boxe/94208/3 "2006-04-25T17:22:03Z")

</div>

> i would like to know how to add a ‘selected option’ for selec box?

I assume you are looking to have the select box automatically select  
the recipe category if it already exists? If that’s the case, you need  
to change your helper to the following:

\<%= select(‘recipes’, ‘category\_id’, Category.find\_all.collect {|c|  
[c.nom, c.id]}) %\>

The select helper method will call the category\_id method on your  
@recipes object and automatically add the selected=“selected”  
attribute for the correct option.

As an aside, you might also be interested in my acts\_as\_dropdown  
plugin for helping to make these select boxes cleaner. You can find  
out more about it here: [http://delynnberry.com/pages/acts\_as\_dropdown](http://delynnberry.com/pages/acts_as_dropdown),  
but it basically allows you to do this:

class Category \< ActiveRecord::Base  
acts\_as\_dropdown :text =\> “nom”  
end

\<%= select(‘recipes’, ‘category\_id’, Category.to\_dropdown) %\>

–  
DeLynn B.  
removed\_email\_address@domain.invalid

> **[DeLynn Berry](https://delynnberry.com)**

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [April 25, 2006, 5:40pm UTC](https://www.ruby-forum.com/t/select-boxe/94208/4 "2006-04-25T17:40:30Z")

</div>

Hello Delynn

assume you are looking to have the select box automatically select

> the recipe category if it already exists?

yes 🙂

If that’s the case, you need

> to change your helper to the following:
> 
> \<%= select(‘recipes’, ‘category\_id’, Category.find\_all.collect {|c|  
> [c.nom, c.id]}) %\>

okidoki

> As an aside, you might also be interested in my acts\_as\_dropdown  
> plugin for helping to make these select boxes cleaner. You can find  
> out more about it here: [http://delynnberry.com/pages/acts\_as\_dropdown](http://delynnberry.com/pages/acts_as_dropdown),  
> but it basically allows you to do this:
> 
> class Category \< ActiveRecord::Base  
> acts\_as\_dropdown :text =\> “nom”  
> end
> 
> \<%= select(‘recipes’, ‘category\_id’, Category.to\_dropdown) %\>

ok thanks u
