0

i have a select that take values from database.

<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), { :class => 'selectpicker' } %>

So i get all countries from database. How i can do for add a custom value(for example Any, with value 0), to this select list taken from database ? For example now i have:

<select>
<option value="UK">UK</option>
</select>

and i want get this:

<select>
<option value="0">Any</option>
<option value="UK">UK</option>
</select>

Thanks.

1 Answer 1

2

You can use the either prompt or include_blank options (FormOptionsHelper) as follows:

<%= select_tag :location, options_from_collection_for_select(Country.all, :id, :country_name), :prompt => 'Any', :class => 'selectpicker' %>
Sign up to request clarification or add additional context in comments.

5 Comments

It say: wrong number of arguments (4 for 3), anyway i don't want only show "Any", but also assign a value to this prompt...example Any have value 0.
@Richard, please see the updated answer that fixes the wrong number of arguments. When I tested this I was using f.select which eventually led to copy/paste error. Why do you need this value to be zero, do you want it to be stored in db?
No, i don't store in db...but i want know if the user have choice Any(it's a search form).
Now give me in params: "location"=>"", (when i choice any).
You probably want to add presence validation on that field?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.