2009-03-07 5 views
9

Ruby on Rails의 특정 질문과 관련된 체크 박스를 만드는 올바른 방법은 무엇입니까? 순간 내가 가진 :Rails의 체크 박스

나는 또한 자동으로 이전에 선택한 항목을 확인 할 수 있도록하려는
<div class="form_row"> 
    <label for="features[]">Features:</label> 
    <br><%= check_box_tag 'features[]', 'scenarios' %> Scenarios 
    <br><%= check_box_tag 'features[]', 'role_profiles' %> Role profiles 
    <br><%= check_box_tag 'features[]', 'private_messages' %> Private messages 
    <br><%= check_box_tag 'features[]', 'chatrooms' %> Chatrooms 
    <br><%= check_box_tag 'features[]', 'forums' %> Forums 
    <br><%= check_box_tag 'features[]', 'news' %> News 
    <br><%= check_box_tag 'features[]', 'polls' %> Polls 
</div> 

(이 양식이 된 경우 다시로드). 매개 변수를 기본값으로 어떻게로드합니까?

답변

14

다음을 찾고 있습니다 :

<div class="form_row"> 
    <label for="features[]">Features:</label> 
    <% [ 'scenarios', 'role_profiles', ... , 'polls' ].each do |feature| %> 
     <br><%= check_box_tag 'features[]', feature, 
       (params[:features] || {}).include?(feature) %> 
     <%= feature.humanize %> 
    <% end %> 
</div> 

이미 features 테이블이있는 Feature 모델 및 has_many :features 관계가있는 경우, 당신은 아마이 원하는 있지만 :

<div class="form_row"> 
    <label for="feature_ids[]">Features:</label> 
    <% for feature in Feature.find(:all) do %> 
     <br><%= check_box_tag 'feature_ids[]', feature.id, 
       @model.feature_ids.include?(feature.id) %> 
     <%= feature.name.humanize %> 
    <% end %> 
</div> 
+0

정의되지 않은 지역 변수를 # alamodey

+0

에 대한 'features'메서드는 무엇입니까? 음, 어떤 모델에 기능 연관이 있고 어떤 변수가 저장되어 있습니까? @조? @생성물? :) – vladr

+0

사실, 컨트롤러에서 볼 수있는 _and_ 액션 및 관련 모델의 전체 코드를 게시하지 않으시겠습니까? – vladr