Forum: Ruby on Rails collection_select and select_tag help

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.
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2009-01-02 09:04
I have created the following select box which works fine except for the
fact if you edit the item it defaults back to N/A instead of selecting
the correct location.

select_tag("item[location_id]", '<option value="">N/A</option>' +
options_for_select(@locations.collect{|location| [location.name,
location.id]}))

Ideally, I want to do something like this (which selects the correct
value on edit):

f.collection_select(:locations, @locations, :id, :name) %>

but how can I add an extra blank option at the top like I did with
select_tag?

thanks!
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-01-02 11:14
(Received via mailing list)
On 2 Jan 2009, at 08:04, Scott Kulik wrote:

>
> I have created the following select box which works fine except for
> the
> fact if you edit the item it defaults back to N/A instead of selecting
> the correct location.
>
> select_tag("item[location_id]", '<option value="">N/A</option>' +
> options_for_select(@locations.collect{|location| [location.name,
> location.id]}))
>
The (optional) last argument to options_for_select is the value to be
preselected (ie something like item.location_id)

Fred
Debcfd8f3f533800e2711c704825f9c0?d=identicon&s=25 Scott Kulik (kuliksco)
on 2009-01-02 15:15
>> I have created the following select box which works fine except for
>> the
>> fact if you edit the item it defaults back to N/A instead of selecting
>> the correct location.
>>
>> select_tag("item[location_id]", '<option value="">N/A</option>' +
>> options_for_select(@locations.collect{|location| [location.name,
>> location.id]}))
>>
> The (optional) last argument to options_for_select is the value to be
> preselected (ie something like item.location_id)
>
> Fred

ah didn't see that that.  Thanks!  i got it working.
This topic is locked and can not be replied to.