我已经搜索了整个互联网,但我无法让这些表格正常工作。事情是这样的:
我有两个由 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
我想要做的是使用一个名为 quantidade (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 %>
编辑 1
我想要做的基本想法是这样的:
<!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>