Rails 4 has_many :through with collection_check_boxes and additional join table text_field
Rails 4 has_many :through with collection_check_boxes and additional join table text_field
我已经在整个 Internet 上搜索过了,但我无法使用此表格。事情是这样的:
我有两个模型通过 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
我想要做的是创建一个 collection_check_boxes,其中包含一个名为 quantidade(数量)的嵌套额外 text_field,因为我已经设置了连接 table:
连接的迁移文件 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
但问题是我不知道如何在我的控制器和视图中执行此操作。我试过类似的方法,但没有用。
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>
我并不是说这是完整的答案,但我认为您正在尝试使用嵌套属性和 collection_check_boxes 来保存选定的复选框。它基本上会发送 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
表格(新订单服务)
<%= 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>
我已经在整个 Internet 上搜索过了,但我无法使用此表格。事情是这样的:
我有两个模型通过 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
我想要做的是创建一个 collection_check_boxes,其中包含一个名为 quantidade(数量)的嵌套额外 text_field,因为我已经设置了连接 table:
连接的迁移文件 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
但问题是我不知道如何在我的控制器和视图中执行此操作。我试过类似的方法,但没有用。
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>
我并不是说这是完整的答案,但我认为您正在尝试使用嵌套属性和 collection_check_boxes 来保存选定的复选框。它基本上会发送 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
表格(新订单服务)
<%= 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>