1

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
3
  • What is the "@stock_of_ingredient" instance variable set to? Commented Jun 5, 2015 at 12:46
  • 1
    And can you add some more details of your associations please? Commented Jun 5, 2015 at 12:46
  • Hi Stock_of_ingredient is saving todays date, id of product and quantity there is no association Commented Jun 5, 2015 at 13:20

1 Answer 1

3

As far as I understand what you're doing, I think you can use 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

Railscast close to your question

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Thanks for replay, so should I on the first place modify my form_tag and then add fields_for?
Hi yes you need to make a form_tag specific for your action and make the corresponding route to the controller that will take care of the action. This railscast shows a very similar action, this should help you catching how to use fields for: railscasts.com/episodes/165-edit-multiple-revised
In your controller you will need to implement something like this: Ingredient.update(params[:ingredients].keys, params[:ingredients].values) for updating the records.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.