【问题标题】:Convert grouped_collection_select to Simple Form in Rails dynamic menu?在 Rails 动态菜单中将 grouped_collection_select 转换为简单表单?
【发布时间】: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


    【解决方案1】:

    您应该查看simple_form_for github 页面中的“组”部分。我正在做与您正在做的事情类似的事情,并找到了这个部分。它给了我需要去的方向。我没有使用我在其他部分使用的f.association,而是使用f.input:group_select:group_method

    f.input :country_id, collection: @continents, as: :grouped_select, group_method: :countries

    来自

    simple_form_for github pag

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-15
      • 2012-01-17
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2010-11-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多