Reputation: 23
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 %>
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>
Upvotes: 1
Views: 657
Reputation: 4436
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>
Upvotes: 1