2013-11-21 2 views
0

컬렉션의 항목에 대한 자세한 내용을 표시하려면 어떻게합니까? 내 양식에서 제공 할 수있는 유일한 세부 사항은 확인란 및 텍스트 도우미로 인해 텍스트입니다. 또한 항목의 그림 또는 항목 설명과 같은 세부 정보를 표시하려고합니다.컬렉션에 대한 추가 특성을 표시하는 방법 확인란

현재이 내가

 <%= f.collection_check_boxes(
        :logo_type_ids, LogoType.all, :id, :name, allow_destroy: true 
     ) do |b, c| %> 
      <div class="col-sm-6 col-md-4"> 
      <% logo_type = LogoType.where(id: b.value).first %> 
      <div class="thumbnail"> 
       <%= image_tag(logo_type.example(:original)) %> 
       <div class="caption"> 
       <h3><%= b.label { b.check_box + b.text } %></h3> 
       <p><%= logo_type.description %></p> 
       </div> 
      </div> 
      </div> 
     <% end %> 

작품 위의 코드를하고있는 중이 야하지만 난이 올바른 접근 방식을 생각 해달라고 것입니다. 이것이 어떻게 처리 될 수 있는지에 대한 아이디어. 고맙습니다.

답변

0

두 가지 제안 사항. 먼저 컨트롤러가 모든 LogoType 객체를로드하는지 확인합니다. 그것이 SRP입니다. 이렇게하면 DB에 많은 불필요한 출장을 절약 할 수 있습니다. 특히 아래 코드에서 두 번째 줄은 DB를 누를 때마다 LogoType을 컬렉션에서 꺼냅니다.

<%= f.collection_check_boxes(:logo_type_ids, @logo_types, :id, :name, allow_destroy: true) do |b, c| %> 
    <% logo_type = @logo_types.detect{|logo| logo.id == b.value } %> 
    <div class="col-sm-6 col-md-4"> 
    <div class="thumbnail"> 
     <%= image_tag(logo_type.example %> 
     <div class="caption"> 
     <h3><%= b.label { b.check_box + b.text } %></h3> 
     <p><%= logo_type.description %></p> 
     </div> 
    </div> 
    </div> 
<% end %> 

둘째, 나는 강하게 도우미 메서드에 바깥 쪽 사업부에서 모든 포장 생각 하는데요. 도우미는 logo_type 및 'b'를 허용하고 content_tag를 사용하여 마크 업을 작성하거나 부분을 렌더링 한 다음 logo_type 및 b를 로컬로 부분에 전달할 수 있습니다. 이 옵션은 마크 업을 훨씬 표현력있게 만듭니다.

<%= f.collection_check_boxes(:logo_type_ids, @logo_types, :id, :name, allow_destroy: true) do |b, c| %> 
    <% logo_type = @logo_types.detect{|logo| logo.id == b.value } %> 
    <%= logo_type_collection_checkbox logo_type, b %> 
<% end %>