2016-08-02 1 views
0

프로그래밍 할 때 새로운 기능이며 레일의 루비에서 양식을 만드는 것과 관련된 질문이 있습니다. 나는 제품 단위를 저장하기위한 데이터베이스를 만들고있다. 이 단위는 창작시 초기에 저장소에 있습니다. 이 위치는 단위 모델 열에 저장됩니다. 그런 다음 모델 저장소와 모델 치료를 받았습니다.체크 상자를 사용하여 중첩 된 양식을 사용하여 모델의 각 개체에서 값을 수정하는 방법

class Unit < ActiveRecord::Base 
    belongs_to :store 
    belongs_to :remission 
    attr_accessor :into_remission 
end 

class Remission < ActiveRecord::Base 
    belongs_to :store 
    has_many :units 
    accepts_nested_attributes_for :units 
end 

class Store < ActiveRecord::Base 
    has_many :units 
    has_many :remissions 
end 

스토어 has_many : remissions 및 Remission has_many : units. 상점에 판매 할 단위를 몇 개 놓았을 때 사안을 만들어야한다는 아이디어가 있습니다. 이 면제는 내가 상점에 제공 한 제품 목록이므로 두 제품 모두 상점에있는 상품에 대한 참조를 가질 수 있습니다. 따라서 Remission의 store_id (참조)를 변경할 상점을 선택할 수있는 사안을 작성하는 양식이 있습니다. 또한이 새로운 형태의 remission에 참여할 유닛을이 같은 형태로부터 선택하여 각 유닛에서 remission_id (참조)를 변경하려고합니다. 이를 위해 Unit 모델에서 attar_accessor : into_remissions를 만들었으므로 각 유닛에서 true로 변경할 수 있습니다. 확인란을 선택하면이를 완화 할 수 있습니다.

그는 CHECK_BOX 말할 필요도없이
<div class="row"> 
    <div class="col-md-6 col-md-offset-3"> 
    <%= form_for(@remission) do |f| %> 
     <%= render 'shared/error_messages_remissions' %> 
#Here you select the store that would own the remission and the products 
     <div class="field"> 
     <%= f.label :store_id, "Producto" %> <br/> 
     <%= f.collection_select :store_id, @stores.order(:name), :id, :name, prompt: "Select Store" %> 
     </div> 
#Here its a dynamic table that display all the products in storage 
     <table id="products" class="table table-striped table-bordered" cellspacing="0" width="100%"> 
      <thead> 
      <tr> 
       <th>Product Name</th> 
       <th>Remission</th> 
      </tr> 
      </thead> 
      <tbody> 
     #HERE I WANT TO MAKE A CHECK_BOX THAT CHANGES THE INTO_REMISSION VALUE FROM EACH UNIT. SO IT MARKS THE UNITS THAT WOULD TAKE PART IN THE REMISSION 
      <%= f.fields_for :units do |ff| %> 
      <% @units.each do |unit| %> 
       <% unless unit.sold %> 
       <tr> 
        <td><%= unit.product.name %></td> 
        <td> 
         <%= ff.label :into_remission, "Nombre de tu Marca:" %> 
         <%= ff.check_box :into_remission%> 
        </td> 
       </tr> 
       <% end %> 
      <% end %> 
      <% end %> 
      </tbody> 
     </table> 
     <%= f.submit "Submit", class: "btn btn-primary" %> 
    <% end %> 
    </div> 
</div> 

는 지금 보여주는이나 작업 :이 작업을 많은 문제가 여기 내 양식의 코드 가지고. 레일 및 프로그래밍에 익숙하지 않아서이 작업을 수행하는 방법 및 조언을 환영할만한 것은 아닙니다.

답변

1

이 변경 시도 감사 :이에

<%= f.fields_for :units do |ff| %> 
    <% @units.each do |unit| %> 
    <% unless unit.sold %> 
     <tr> 
     <td><%= unit.product.name %></td> 
     <td> 
      <%= ff.label :into_remission, "Nombre de tu Marca:" %> 
      <%= ff.check_box :into_remission%> 
     </td> 
     </tr> 
     <% end %> 
    <% end %> 
<% end %> 

:

<% @units.each do |unit| %> 
    <%= f.fields_for :units, unit do |ff| %> 
    <% unless unit.sold %> 
     <tr> 
     <td><%= unit.product.name %></td> 
     <td> 
      <%= ff.label :into_remission, "Nombre de tu Marca:" %> 
      <%= ff.check_box :into_remission%> 
     </td> 
     </tr> 
     <% end %> 
    <% end %> 
<% end %> 

내가 먼저 단위를 반복하고의 필드에 개별적으로 각각의 유닛을 통과한다고 생각합니다. 그것은 보여주는 문제를 해결해야합니다.

관련 문제