【发布时间】:2013-02-17 23:36:48
【问题描述】:
我有以下(和有效的)动态菜单/下拉菜单,允许您选择属性类型,然后选择具有常规导轨形式的属性子类型:
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()
_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 %>
这很好用。但是,我想将其集成到已经使用 simple form gem 设置的更大的应用程序中。我还通过bootstrap-sass 使用 twitter 引导程序。
我能得到的最接近的表格是:
<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>
注意:我必须将 :prop_type_id 更改为 :prop_type 以防止应用程序抛出错误。
但这不起作用 - 第二个下拉菜单不会映射到第一个。我在我的 java/coffeescript 中做错了什么?对于第二个下拉菜单,是否有“grouped_association”之类的东西或类似的东西?
这是否可行,或者我应该将整个表单转换回标准 rails 格式?
更新
我能够让它工作,但将 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>
【问题讨论】:
标签: ruby-on-rails drop-down-menu associations simple-form bootstrap-sass