简体   繁体   English

如何使用rails中的一个表单更新多个字段

[英]How to update more than one field using one form in rails

I would like to update my stock check every day. 我想每天更新我的库存支票。 So I have Ingredient table in my database also have IngredietnStockChek where every day I would like to save my stock check. 所以我在我的数据库中有成分表也有IngredietnStockChek每天我想保存我的库存检查。 My idea is to create form who will show me input field quantity for every product I've got and then on this form I'll put how much I have. 我的想法是创建一个表格,它会显示我已经获得的每个产品的输入字段数量,然后在这个表格上我会说明我有多少。

    <%= 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 %>   

So this is my form should I use 所以我应该使用这个表格

 <%= fields_for "stock_of_ingredients" do |c| %>`

Or should I 或者我应该

 <%= fields_for "stock_of_ingredients[]" do |c| %>

To catch all data from all fields 捕获所有字段的所有数据

And how to modify controller create action to save all, I should probably use loop but how. 而如何修改控制器创建动作以保存所有,我应该使用循环,但如何。

 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

As far as I understand what you're doing, I think you can use fields_for: 据我了解你在做什么,我认为你可以使用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 %>

Documentation for fields_for fields_for的文档

Railscast close to your question Railscast接近你的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM