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. 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