Rails collection_select default select -
Rails collection_select default select -
in rails 4 in view have
<%= form_for @person |f| %> <%= f.collection_select :country_id, country.order(:name), :id, :name, include_blank: "select country" %> ... <% end %>
i'd "select country" selected default whenever page loaded. 1 way utilize javascript (select after dom loaded). there easier way adding alternative collection_select
?
thanks.
as per the docs, it's prompt
alternative in options
argument:
collection_select(:post, :author_id, author.find(:all), :id, :name_with_initial, {:prompt => 'please select author of post'})
with collection_select
on form builder omit first argument, in case:
f.collection_select :country_id, country.order(:name), :id, :name, {prompt: 'select country'}
i've 100% confirmed working on own app running rails 4.1.6, prompt
, include_blank
same thing.
the way works rails injects null-valued <option>
first item in generated <select>
(this because html spec has nil analogous placeholder
on text inputs select inputs).
reasons may fail:
rails not mark prompt alternativeselected
attribute, , suspect browsers may take render own blank entry instead of first in list if, existing records, rails determines current record's country_id
matches element in list mark 1 selected
. expected behaviour can pain if you're doing non-standard. if you're beingness bitten these problems options build form manually (the method options_from_collection_for_select
may of utilize here) or in javascript. there undocumented default
attribute can add together <option>
tag it's not in spec , browser back upwards may patchy, , you'd still have build form manually.
ruby-on-rails select selected collection-select
Comments
Post a Comment