Forum: Ruby on Rails dropdown

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.
A71bcb92b36f48a83f06f9f0b1273990?d=identicon&s=25 Greg N. (gregnewman)
on 2006-06-05 13:11
I'm using a dropdown on my form for a simple cms which displays the
pages from the database.  I want to add some options to the dropdown
that are not in the database.  Is there a way to do this?

Here's my code so far:

<p><label for="navigation_link">Link</label><br/>
<%= select("navigation", "link", Page.find_all.collect {|p| [ p.title,
p.id ] }) %></p>

What I want is to add in a link called "listings" that is not in the
table as well as a link called "home".  I'm giving the user the option
to choose the display order, so it doesn't matter where they show in the
dropdown.  However, it would be nice to have it still autoselect the
appropriate item in the edit form.
6d57db44c5a4e0721cc7f78cf0ce308a?d=identicon&s=25 Stephen Bartholomew (steveb)
on 2006-06-05 13:19
try something like this:
<select>
 <option value="home">Home</option>
<%= option_groups_from_collection_for_select Page.find_all, 'id',
'title' %>
</select>

See:
http://api.rubyonrails.org/classes/ActionView/Help...
for more details.

Hope that helps,

Steve

Greg Newman wrote:
> I'm using a dropdown on my form for a simple cms which displays the
> pages from the database.  I want to add some options to the dropdown
> that are not in the database.  Is there a way to do this?
>
> Here's my code so far:
>
> <p><label for="navigation_link">Link</label><br/>
> <%= select("navigation", "link", Page.find_all.collect {|p| [ p.title,
> p.id ] }) %></p>
>
> What I want is to add in a link called "listings" that is not in the
> table as well as a link called "home".  I'm giving the user the option
> to choose the display order, so it doesn't matter where they show in the
> dropdown.  However, it would be nice to have it still autoselect the
> appropriate item in the edit form.
A71bcb92b36f48a83f06f9f0b1273990?d=identicon&s=25 Greg N. (gregnewman)
on 2006-06-05 14:07
Worked like a charm.  Thanks Steve!


Stephen Bartholomew wrote:
> try something like this:
> <select>
>  <option value="home">Home</option>
> <%= option_groups_from_collection_for_select Page.find_all, 'id',
> 'title' %>
> </select>
>
> See:
> http://api.rubyonrails.org/classes/ActionView/Help...
> for more details.
>
> Hope that helps,
>
> Steve
This topic is locked and can not be replied to.