0

난 당신이 정기적으로 레일 속성 입력 한 다음 속성 하위 유형을 선택할 수 있습니다 다음 (및 작업) 동적 메뉴/드롭 다운 형성했다 :grouped_collection_select를 Simple Form in 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() 

_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을 통해 트위터 부트 스트랩을 사용하고 있습니다.

내 형태로 얻을 수있는 가장 가까운입니다

<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 오류를 던지고에서 응용 프로그램을 유지하기 위해.

하지만 작동하지 않습니다. 두 번째 드롭 다운은 첫 번째 드롭 다운으로 매핑되지 않습니다. 내 자바/coffeescript에서 뭔가 잘못하고 있는거야? 두 번째 드롭 다운에서 'grouped_association'또는 그 행에있는 것이 있습니까?

이 작업을 수행 할 수 있습니까, 아니면 전체 양식을 다시 표준 레일 형식으로 변환해야합니까?

나는 그것을 얻을 수 있었다

작동하려면 UPDATE하지만 다음과 같이 div의에 ERB를 고집 :

<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

에서 :group_select:group_method

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

f.input를 사용하여 종료

관련 문제