Forum: Ruby on Rails select boxe

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Bolo (Guest)
on 2006-04-25 18:17
(Received via mailing list)
yop,

http://rubyonrails.org/api/classes/ActionView/Help...

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
Bolo (Guest)
on 2006-04-25 19:03
(Received via mailing list)
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>:
DeLynn B. (Guest)
on 2006-04-25 19:22
(Received via mailing list)
> 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.
removed_email_address@domain.invalid
http://delynnberry.com
Bolo (Guest)
on 2006-04-25 19:40
(Received via mailing list)
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,
> 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
This topic is locked and can not be replied to.