是否有可能以某种方式使用:selected选项,您可以在带有grouped_collection_select函数的普通选择视图助手上使用?我想设置在我的列表中预先选择的值.我试过传递:选择没有运气的选项!
这是我测试的一些代码片段:
grouped_collection_select 'user[subscription_attributes]', :subscription_plan_id, Trade.order(:name).all, :subscription_plans, :name, :id, :display_name, { :include_blank => true, :selected => 5 }
grouped_collection_select 'user[subscription_attributes]', :subscription_plan_id, Trade.order(:name).all, :subscription_plans, :name, :id, :display_name, :include_blank => true, :selected => 5
Run Code Online (Sandbox Code Playgroud)
两个版本都不起作用.没有选择设置.我正在使用它为嵌套模型设置一个值.我正在使用railscasts动态选择列表方法:http://railscasts.com/episodes/88-dynamic-select-menus-revised
我无法使用formtastic与组选择很好地玩,所以我必须手动完成,但是当用户验证失败时我不会保留此值.我想在修复验证错误时保留此设置.
validation ruby-on-rails ruby-on-rails-3 grouped-collection-select
我终于想出了如何使用本教程实现动态选择菜单.
一切正常,但是如何按名称组织下拉列表中的城市....
以下是我写的所有代码.(如果您需要任何进一步的信息,请告诉我)
新的铁路请帮助:)
VIEWS
<%= simple_form_for ([@book, @rating]) do |f| %>
<div class="field">
<%= f.collection_select :state_id, State.order(:name), :id, :name, {:include_blank=> "Select a State"}, {:class=>'dropdown'} %>
</div>
### I would like the order of the cities displayed in the drop down to be alphabetized
<div class="field">
<%= f.grouped_collection_select :city_id, State.order(:name), :cities, :name, :id, :name, {:include_blank=> "Select a City"}, {:class=>'dropdown'} %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)