我想每天更新我的库存支票。所以我的数据库中有成分表,还有IngredietnStockChek,我每天都想保存我的库存支票。我的想法是创建一个表格,该表格将向我显示我所拥有的每种产品的输入字段数量,然后在此表格上输入我所拥有的数量。
<%= form_for(@stock_of_ingredient, :multipart => true, html: { class: "form-horizontal", role: "form" }) do |f| %>
<% @ingredients.each do |b| %>
<%= fields_for "stock_of_ingredients" do |c| %>
<div class="form-group">
<%= c.label "#{b.name}", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= c.number_field :ingredient_id, class: "form-control", value: "#{b.id}" %>
</div>
</div>
<div class="form-group">
<%= c.label :quantity, class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= c.text_field :quantity, class: "form-control" %>
</div>
</div>
<div class="form-group">
<%= c.label :todays_date, class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= c.date_select :todays_date, class: "form-control" %>
</div>
</div>
<% end %>
<% end %>
所以这是我的表格
<%= fields_for "stock_of_ingredients" do |c| %>`
还是我应该
<%= fields_for "stock_of_ingredients[]" do |c| %>
捕获所有字段中的所有数据
以及如何修改控制器创建动作以保存所有内容,我可能应该使用循环,但如何使用。
def create
@stock_of_ingredient = StockOfIngredient.new(stock_of_ingredient_params)
respond_to do |format|
if @stock_of_ingredient.save
format.html { redirect_to @stock_of_ingredient, notice: 'Stock of ingredient was successfully created.' }
format.json { render :show, status: :created, location: @stock_of_ingredient }
else
format.html { render :new }
format.json { render json: @stock_of_ingredient.errors, status: :unprocessable_entity }
end
end
end
据我了解您在做什么,我认为您可以使用fields_for:
<%= form_tag update_my_ingredients_for_a_product_path, method: :put do %>
<% @ingredients.each do |b| %>
<%= fields_for "ingredients[]", b do |f| %>
<div class="form-group">
<%= f.label "#{b.name}", class: "col-sm-2 control-label" %>
<div class="col-sm-10">
<%= f.number_field :ingredient_id, class: "form-control", value: "#{b.id}" %>
</div>
</div>
...
<% end %>
<%= submit_tag "Save", class: "btn btn-default" %>
<% end %>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句