2

In Rails 4 in view I have

<%= form_for @person do |f| %>
  <%= f.collection_select :country_id, Country.order(:name), :id, :name, include_blank: "Select your country" %>
  ...
<% end %>

I'd like "Select your country" to be selected as default whenever the page is loaded. One way is to use javascript (select it after the dom is loaded). Is there an easier way like adding an option to collection_select?

Thanks.

1 Answer 1

5

As per the docs, it's the prompt option in the options argument:

collection_select(:post, :author_id, Author.find(:all),
                  :id, :name_with_initial,
                 {:prompt => 'Please select the author of this post'})

With collection_select on a form builder we omit the first argument, so in this case:

f.collection_select :country_id, Country.order(:name), :id, :name, {prompt: 'Select your country'}

I've 100% confirmed this as working on my own app running Rails 4.1.6, where prompt and include_blank do the same thing.

The way this works is Rails injects a null-valued <option> as the first item in the generated <select> (this is because the HTML spec has nothing analogous to placeholder on text inputs for select inputs).

Reasons this may fail:

  1. Rails does not mark the prompt option with the selected attribute, and I suspect some browsers may choose to render their own blank entry instead of the first in the list
  2. If, for existing records, Rails determines that the current record's country_id matches an element in the list it will mark that one as selected. This is expected behaviour but can be a pain if you're doing anything non-standard.

If you're being bitten by these problems your options are to build the form manually (the method options_from_collection_for_select may be of use here) or do it in javascript. There is also an undocumented default attribute you can add to an <option> tag but it's not in the spec and browser support may be patchy, and you'd still have to build the form manually.

7
  • Thanks for Googling for me but as per your very first answer, it doesn't work (Obviously I had done my Googling and there is also include_blank: "...", but whenever you refresh the page, the option that was selected last before refresh is selected). Commented Oct 8, 2014 at 3:26
  • @user2725109 Try by writing like this: <%= f.collection_select :country_id, Country.order(:name), :id, :name, {include_blank: "Select your country"} %>
    – anusha
    Commented Oct 8, 2014 at 5:13
  • Thanks for your comment. Tried your format (placing include_blank: "Select your country" inside {}), but it didn't fix the problem, after refresh the last option before refresh is selected. Commented Oct 8, 2014 at 7:25
  • @user2725109: is it not displaying in the select box when the page loaded
    – anusha
    Commented Oct 8, 2014 at 8:09
  • @SLD. Thanks very much for your response. I tried it but it doesn't work. include_blank: 'Prompt text' only works as options but not html_options. Tried all different variations but no success. Commented Oct 8, 2014 at 19:09

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.