RoR select_tag default value & options

Viewed 68868

How can I set a default value using select_tag, and how can I keep the options open on page load?

5 Answers

For options_for_select

<%= select_tag("products_per_page", options_for_select([["20",20],["50",50],["100",100]],params[:per_page].to_i),{:name => "products_per_page"} ) %>

For options from collection for select

<%= select_tag "category","<option value=''>Category</option>" +  options_from_collection_for_select(@store_categories, "id", "name",params[:category].to_i)%>

Note that the selected value which you are specifying must be of type value. i.e. if value is in integer format then selected value parameter should also be integer.

Related