如何使用复选框使用嵌套表单修改模型的每个对象的值
How to use a checkbox to modify a value from each object of a model using a nested form
我是编程新手,我有一个关于在 rails 中制作表单的问题 ruby。我正在制作一个用于存储产品单位的数据库。这些单元在创建时最初位于存储中。此位置存储在一列单元模型中。然后我有一个模型 Store 和一个模型 Remission。
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
Store has_many :remissions 和 Remission has_many :units。我的想法是,当我在商店中放置一些单位进行销售时,我必须创建一个减免。这份免责声明是我给商店的产品清单,这样我们俩都可以参考商店里有哪些产品。所以我有一个表格来创建一个缓解,您可以在其中 select 将更改缓解的 store_id(reference) 的商店。我还想从同一个表格中 select 将参与此新缓解的单位更改每个单位的 remission_id(参考)。为此,我在单元模型中创建了一个 attar_accessor :into_remissions ,这样我就可以在每个单元中将其更改为 true ,这些单元最终会出现在带有复选框的缓解中。我在完成这项工作时遇到了很多问题,这是我的表单代码:
<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>
不用说他 check_box 它现在显示也没有工作。
我不确定如何进行这项工作,欢迎任何建议,因为我是 rails 和编程的新手。谢谢
尝试改变这个:
<%= 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 %>
我认为您应该首先遍历单元并将每个单元分别传递到字段中。这应该可以解决显示问题。
我是编程新手,我有一个关于在 rails 中制作表单的问题 ruby。我正在制作一个用于存储产品单位的数据库。这些单元在创建时最初位于存储中。此位置存储在一列单元模型中。然后我有一个模型 Store 和一个模型 Remission。
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
Store has_many :remissions 和 Remission has_many :units。我的想法是,当我在商店中放置一些单位进行销售时,我必须创建一个减免。这份免责声明是我给商店的产品清单,这样我们俩都可以参考商店里有哪些产品。所以我有一个表格来创建一个缓解,您可以在其中 select 将更改缓解的 store_id(reference) 的商店。我还想从同一个表格中 select 将参与此新缓解的单位更改每个单位的 remission_id(参考)。为此,我在单元模型中创建了一个 attar_accessor :into_remissions ,这样我就可以在每个单元中将其更改为 true ,这些单元最终会出现在带有复选框的缓解中。我在完成这项工作时遇到了很多问题,这是我的表单代码:
<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>
不用说他 check_box 它现在显示也没有工作。 我不确定如何进行这项工作,欢迎任何建议,因为我是 rails 和编程的新手。谢谢
尝试改变这个:
<%= 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 %>
我认为您应该首先遍历单元并将每个单元分别传递到字段中。这应该可以解决显示问题。