Ryzal Yusoff
Ryzal Yusoff

Reputation: 1047

Select a blank option in the static drop-down rails form

I have a drop-down menu of a rails form which the user can have the option to choose between 1 to 5. And I want the drop-down to select the blank option at first.

So I tried this:

<%= f.select :point1, [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]], :include_blank => true %>

But it didn't work. Any idea?

Thanks!

Upvotes: 1

Views: 1322

Answers (2)

stevec
stevec

Reputation: 52268

Here's what worked for me:

<%= f.select :country, countries, {include_blank: true} %>

And here's what it could look like if you had HTML arguments to pass through as well:

<%= f.select :country, countries, {include_blank: true}, {required: true} %>

Note the reason for the two hashes is because the first is for rails options and the second is for HTML options

A more extensive example:

<%= f.select :country, countries, {include_blank: true, prompt: "United States"}, {required: true, class: "form-control"} %>

An example of the 'two hashes' in rails docs:

<% form_for :article, @article, :url => { :action => "create" }, :html => {:class => "nifty_form"} do |f| %>
  <%= f.text_field :title %>
  <%= f.text_area :body, :size => "60x12" %>
  <%= submit_tag "Create" %>
<% end %>

And one more example here

If you accidentally place html options in with the first hash (rails options), then they simply won't get used, so take care to use both hashes!

Upvotes: 1

Pardeep Dhingra
Pardeep Dhingra

Reputation: 3946

Try this:

<%= f.select :point1, options_for_select([[1, 1], [2, 2], [3, 3], [4, 4], [5, 5]]), {:include_blank => true} %>

Upvotes: 3

Related Questions