我已经搜索了整个互联网,但我无法使这些表单正常运行。事情就是这样:
我有两个模型由has_many连接:通过连接模型:
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
加入模型mpm_est.rb
class MpmEst < ActiveRecord::Base
belongs_to :ordem_de_servico
belongs_to :estoque
end
我想要做的是使用一个名为quantdade(quantity)的嵌套额外text_field创建一个collection_check_boxes,因为我已经设置了连接表:
连接表的迁移文件(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
但问题是我不知道如何在我的控制器和视图中执行此操作。我尝试过类似的东西,但它没有用。
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
并在我的ordem_de_servico _form视图中:
<%= 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 %>
我想要做的基本想法是这样的:
<!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>
&#13;
答案 0 :(得分:0)
我不是说这是完整的答案,但我认为您正在尝试使用嵌套属性和collection_check_box保存选定的复选框。它基本上会发送一个estoque_ids数组,如果是这样,下面的代码就可以了,你可以用它作为参考来进一步修改。
迁移(将类型指定为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
表格(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 %>
控制器(根据需要允许参数)
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
编辑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>