cpu*_*ley 0 ruby-on-rails associations simple-form drop-down-menu bootstrap-sass
我有以下(和工作)动态菜单/下拉列表,它允许您选择属性类型,然后选择具有常规rails形式的属性子类型:
properties.js.coffee
jQuery ->
prop_sub_types = $('#property_prop_sub_type_id').html()
$('#property_prop_type_id').change ->
prop_type = $('#property_prop_type_id :selected').text()
escaped_prop_type = prop_type.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
options = $(prop_sub_types).filter("optgroup[label='#{escaped_prop_type}']").html()
if options
$('#property_prop_sub_type_id').html(options)
else
$('#property_prop_sub_type_id').empty()
Run Code Online (Sandbox Code Playgroud)
_form.html.erb
<%= form_for(@property) do |f| %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :prop_type_id, 'Property Type' %><br />
<%= f.collection_select :prop_type_id, PropType.order(:name), :id, :name, :prompt => "-- Select Property Type --" %>
</div>
<div class="field">
<%= f.label :prop_sub_type_id, 'Property Subtype' %><br />
<%= f.grouped_collection_select :prop_sub_type_id, PropType.order(:name), :prop_sub_types, :name, :id, :name, :prompt => "-- Select Property Subtype --" %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Run Code Online (Sandbox Code Playgroud)
这很好用.但是,我想将它集成到一个已经使用简单形式 gem 设置的更大的应用程序中.我也通过bootstrap-sass使用twitter bootstrap .
我能在表格中得到的最接近的是:
<div class="field">
<%= f.association :prop_type, :input_html => { :id => "prop_type_id", :class => "span5" }, :prompt => "-- Select Property Type --" %>
<%= f.association :prop_sub_type, :input_html => { :id => "prop_sub_type_id", :class => "span5" }, :prompt => "-- Select Property Subtype --" %>
</div>
Run Code Online (Sandbox Code Playgroud)
注意:我必须将:prop_type_id更改为:prop_type以防止应用程序抛出错误.
但这不起作用 - 第二次下拉不会映射到第一次.我在java/coffeescript中做错了什么?对于第二个下拉列表,是否存在'groups_association'或者其他类似的东西?
这甚至是可行的,还是应该将整个表格转换回标准的轨道格式?
UPDATE
我能够让它工作,但将erb坚持到div中,如下所示:
<div class="field">
<%= f.collection_select :prop_type_id, PropType.order(:name), :id, :name, :prompt => "-- Select Property Type --" %>
</div>
<div class="field">
<%= f.grouped_collection_select :prop_sub_type_id, PropType.order(:name), :prop_sub_types, :name, :id, :name, :prompt => "-- Select Property Subtype --" %>
</div>
Run Code Online (Sandbox Code Playgroud)
您应该查看simple_form_forgithub页面中的Group部分.我正在研究类似于你正在做的事情并找到了这一部分.它给了我我需要去的方向.而不是做的f.association,我在其他地方使用,我结束了使用f.input与:group_select和:group_method
f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries
从
| 归档时间: |
|
| 查看次数: |
3067 次 |
| 最近记录: |