Bambi's Dilemma Bambi's Dilemma - 3 months ago 5
Ruby Question

How to use a checkbox to modify a value from each object of a model using a nested form

I'm new at programming and I have a question related to making a form in ruby in rails. I'm making a database for storing products' units. These units are, upon creation, initially located at the storage. This location is stored in a column Unit model. Then I have a model Store and a model 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 and Remission has_many :units. The idea is that when I put some units for selling in a store I have to create a remission. This remission is the list of products that I gave the store so we both can have a reference of which products are at the store. So I have a form to create a remission in which you can select the store that would change the store_id(reference) of the Remission. I also want to select from this same form the units that would take part in this new remission changing the remission_id(reference) from each unit. For that I made a attar_accessor :into_remissions in the Unit model so I could change that to true in each unit that would end up in the remission with a checkbox. I having a lot of problems making this work, here is the code of my form:

<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>


Needless to say he check_box its now showing nor working.
I not sure how to make this work and any advices would be welcome as I'm new with rails and programming. Thanks

Answer

Try changing this:

<%= 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 %>

into this:

<% @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 %>

I think you should first iterate over the units and pass each unit separately into the fields for. That should solve the showing issue.

Comments