1

I've already searched about the whole internet but I can't get this forms working. Here's the thing:

I have two models connected by a has_many :through join model:

estoque.rb

class Estoque < ActiveRecord::Base
  has_many :mpm_ests
  has_many :ordem_de_servicos, :through => :mpm_ests, dependent: :destroy
end

ordem_de_servico.rb

class OrdemDeServico < ActiveRecord::Base
  has_many :mpm_ests, dependent: :destroy
  has_many :estoques, :through => :mpm_ests
  accepts_nested_attributes_for :mpm_ests
end

And the join model mpm_est.rb

class MpmEst < ActiveRecord::Base
  belongs_to :ordem_de_servico
  belongs_to :estoque
end

What I want to do is make a collection_check_boxes with a nested extra text_field called quantidade (quantity), as I've setup the join table:

migration file of the join table (mpm_est):

class CreateMpmEsts < ActiveRecord::Migration
  def change
    create_table :mpm_ests do |t|
    t.integer :ordem_de_servico_id
    t.integer :estoque_id
    t.string :quantidade
  end
    add_index :mpm_ests, :ordem_de_servico_id
    add_index :mpm_ests, :estoque_id
    add_index :mpm_ests, [:ordem_de_servico_id, :estoque_id], unique: true
  end
end

But the problem is I have no idea how to do this in my controller and view. I've tried something like this, but it didn't work.

ordem_de_servicos_controller.rb

def new
  @ordem_de_servico = OrdemDeServico.new
  @ordem_de_servico.mpm_ests.build
end

def edit
  @ordem_de_servico.mpm_servs.build
  @ordem_de_servico.mpm_ests.build
end

[...]

 def ordem_de_servico_params
      params.require(:ordem_de_servico).permit(:cliente_id, :veiculo, :placa, :mecanico_id, {:estoque_ids => []}, :quantidade, :prazo, :pago, :valor_pago, :historico_pgto, :status)
    end

and in my ordem_de_servico _form view:

<%= f.fields_for :mpm_ests do |ff| %>
      <%= ff.collection_check_boxes(:ordem_de_servico, :estoque_ids, Estoque.all, :id, :nome) %>
      <%= ff.text_field :quantidade %><br>
<% end %>

Edit 1

The basic idea what I want to do is something like this:

<!DOCTYPE html>
<html>
<body>

<h1>Ordem De Servico (Service)</h1>

<label>Number<label>
<input type="text">

<label>Service<label>
<input type="text">

<label>Person<label>
<input type="text">

<h5>Inventory (estoque)</h5>
<form action="">
<input type="checkbox" name="vehicle" value="Bike">Iron   <label>Quantity<label><input type="text"><br>
<input type="checkbox" name="vehicle" value="Car">copper   <label>Quantity<label><input type="text"><br>
<br><button>Save Ordem de Servico (service)</button>
</form>

</body>
</html>
balataca
  • 23
  • 5

1 Answers1

1

I don't say this is complete answer but I think you are trying to save selected check boxes using nested attributes and collection_check_boxes. It will basically send array of estoque_ids, if thats the case, below code will work and you can use it as reference to modify further.

Migration (specify type as array for estoque_ids)

class CreateMpmEsts < ActiveRecord::Migration
  def change
    create_table :mpm_ests do |t|
      t.integer :ordem_de_servico_id 
      t.string :quantidade
      t.text :estoque_ids, array: true, default: []

      t.timestamps null: false
    end
  end
end

form (New Ordem De Servico)

<%= f.fields_for :mpm_ests do |ff| %> 
      <%= ff.collection_check_boxes(:estoque_ids, Estoque.all, :id, :name) do |b|
          b.label { b.check_box }
        end
        %>
      <%= ff.text_field :quantidade %><br>
<% end %>

Controller (Permit params as required)

def create
    @ordem_de_servico = OrdemDeServico.new(ordem_de_servico_params)

    respond_to do |format|
      if @ordem_de_servico.save
        format.html { redirect_to @ordem_de_servico, notice: 'Ordem de servico was successfully created.' }
        format.json { render :show, status: :created, location: @ordem_de_servico }
      else
        format.html { render :new }
        format.json { render json: @ordem_de_servico.errors, status: :unprocessable_entity }
      end
    end
  end
private
def ordem_de_servico_params
  params.require(:ordem_de_servico).permit! #permit params u need
end

EDIT 1:

  <div class="field">
      <% Estoque.all.each_with_index do |es,index| %>
            <div>
                <%= check_box_tag "ordem_de_servico[mpm_ests_attributes][#{index}][estoque_id]", es.id%>
                <%= es.name %>
                <%= text_field_tag "ordem_de_servico[mpm_ests_attributes][#{index}][quantidade]", :quantidade %><br>
            </div>
        <% end %>  
  </div>
Dave
  • 4,376
  • 3
  • 24
  • 37
  • Thanks for your fast answear, your code worked perfectly. However, this piece of code stores in the database an array with all estoque_ids in one field and, consequently, one record. What I wanted to do was for each relationship (estoque_id and ordem_de_servico_id) it stores a different quantity. For example, what your suggestion do is in one record I can store just one quantity for the selected check boxes estoque_ids, but I need to store a quantity for each estoque_id in the join table. – balataca Sep 17 '15 at 00:51
  • Check my edit part on answer... Are you expecting same behaviour with collection_check_boxes ? ... Let me know on this :) – Dave Sep 17 '15 at 17:37
  • It would be better if I could somehow build this `@ordem_de_servico.mpm_ests.build` with the paramter :quantidade in controller, because having the field estoque_ids as text and doing what you suggested, unfortunately, isn't recording one id for one quantity in DB. So what I have in mind is having a table with ordem_de_servico_id(integer) estoque_id(integer) and quantidade(string) and let rails build the link of collection_check_boxes with the text_field and saving this paramters together in DB. Thank you. – balataca Sep 18 '15 at 00:41
  • I tried to make an example of what I need, please see my edit 1 in my post. Thanks a lot. – balataca Sep 18 '15 at 15:32
  • Did you check my code in Edit1 .. It does save relationship individually like in your Question (Edit 1) .. Let me know if that is not correct or what alteration you need on it .... Just put my edit 1 code in answer in your form view and check it will save Estoque id, OrdemDeServico id and quantidade as a single record .. Do u want to do the same with collection_check_boxes ? – Dave Sep 18 '15 at 16:03
  • it works pretty good your code thanks. However, when I edit a ordem_de_servico, the fields quantidade aren't loaded as values in the view from DB. I've already fixed the estoque_ids edit by doing this @ordem_de_servico.estoque_ids.include?(es.id), but the field quantidade is not loading properly. How can I fix this? – balataca Sep 20 '15 at 20:52
  • any ideas about quantidade value showing on edit view? – balataca Sep 22 '15 at 14:01