0

我有以下的(和工作)的動態菜單/下拉菜單,讓你選擇一個正規的軌道屬性類型,然後屬性亞型形成:將Rails動態菜單中的grouped_collection_select轉換爲簡單表單?

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寶石一起設置的更大的應用程序中。我也通過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> 

:我不得不改變:prop_type_id到:prop_type保持該應用從引發錯誤。

但這不起作用 - 第二次下拉不會映射到第一個。我在我的java/coffeescript中做錯了什麼?第二個下拉列表中有沒有像'group_association'之類的東西?

這是甚至是可行的,還是應該將整個表單轉換回標準導軌格式?

UPDATE

我能得到它的工作,但堅持再培訓局到的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> 

回答

7

你應該看看在simple_form_for GitHub的頁面組部分。我正在研究類似於你正在做的事情,並發現了這一部分。它給了我需要去的方向。而不是做f.association我在其他地方使用,我結束了從

simple_form_for github pag

使用 f.input:group_select:group_method

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