Forum: Ruby on Rails Default value in select_tag

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.
Valentino L. (Guest)
on 2009-04-09 08:29
Dear all

I have the following select_tag in view

<%= select_tag(:event_status, options_for_select([""]) +
options_from_collection_for_select(@event_status_list, :value, :detail))
%>

which give the following drop down item
""
Ignore
Problem
Normal
Handling

I want to set the default value to "Problem". I have used google but
cannot find my desired output.

I try the following, but failed...

<%= select_tag(:event_status, options_for_select([""]) +
options_from_collection_for_select(@event_status_list||"Problem",
:value, :detail))  %>

Can anyone give me some hints.

Thank you.
Valentino
Phlip (Guest)
on 2009-04-09 08:35
(Received via mailing list)
Valentino L. wrote:

> <%= select_tag(:event_status, options_for_select([""]) +
> options_from_collection_for_select(@event_status_list, :value, :detail))
> %>

The trick is recognizing options_from_collection_for_select(), not
select_tag(),
creates the <options>, and only they can use the selected='selected'
attribute.

Google for options_from_collection_for_select() and "default value"...
Jaryl Sim (Guest)
on 2009-04-09 10:54
(Received via mailing list)
<%= select_tag(:event_status, options_for_select([""]) +
options_from_collection_for_select
(@event_status_list||"Problem", :value, :detail, @selected_status))
%>

See
http://api.rubyonrails.org/classes/ActionView/Help...
Jaryl Sim (Guest)
on 2009-04-09 10:56
(Received via mailing list)
Hadn't realized that you were trying to specify "Problem" as the
default. Note that if your :value method gives another result for
"Problem", like for example, 1, then you have to change the selected
field accordingly.

<%= select_tag(:event_status, options_for_select([""]) +
options_from_collection_for_select
(@event_status_list, :value, :detail, "Problem"))
%>
This topic is locked and can not be replied to.