Select boxe

yop,

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

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 [email protected]:

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,
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.
[email protected]

Hello Delynn

assume you are looking to have the select box automatically select

the recipe category if it already exists?

yes :slight_smile:

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,
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