frenciaj
frenciaj

Reputation: 128

Create multiple records with a simple form on Rails

I'm creating an inventory app, and one of the features is to change the stock inventory in bulk, so when you go to the Stocks link, it will show you a full list of items with 2 fields, INs and OUTs, once the user completes this and saves, it will be stored in the stock table (which has IN OUT and item_id fields, and created_at and updated_at)

So far i have been struggling with this, since all the examples i have been looking focus more on the item itself and not on the stock part (on my case)

Here is what i have so far:

this is the _form.erb on the stocks folder

<% @items.each do |item| %>
 <%= form_for item do |f| %>
  <%= f.fields_for :stocks, [Stock.new] do |stock| %>
   <%= f.label item.nombre %>
   Altas <%= stock.text_field :altas %>
   Bajas <%= stock.text_field :bajas %>
   <%= stock.hidden_field :items_id, :value => item.id %>
   <% end %>
  <%= f.submit %>
 <% end %>
<% end %>

I didnt do anything other than the scaffold on the controllers

and the models has this: stock.rb

class Stock < ActiveRecord::Base
 belongs_to :items
end

item.rb

class Item < ActiveRecord::Base
 has_many :stocks
 attr_accessible :nombre, :espesor, :material, :quantity
 accepts_nested_attributes_for :stocks
 attr_accessible :stocks_attributes
 self.per_page = 50

 protected
end

So far running this code, will show me each item name with both fields but also an update button for each item, and running the code will give me an error in the controller

unknown attribute 'item_id' for Stock.

and the following param is what im getting:

{"utf8"=>"✓",
"_method"=>"patch",
   "authenticity_token"=>"xAdfWBvf4MdkeiZ8xR5ElkNaMMTB5ySt1C9nCG5iGw2hwgDs1MJ2luLH2slvaEEIQBgjac4+RkxZIg6InIbZ1A==",
"item"=>{"stocks_attributes"=>{"0"=>{"altas"=>"7",
"bajas"=>"8",
"items_id"=>"4"}}},
"commit"=>"Update item",
"id"=>"4"}

so its only storing one value and not all 4 (on this case are 4 items)

Here is the schema: ActiveRecord::Schema.define(version: 20160824232702) do

create_table "items", force: :cascade do |t|
 t.string   "nombre"
 t.integer  "espesor"
 t.string   "material"
 t.integer  "quantity"
 t.datetime "created_at"
 t.datetime "updated_at"
end

create_table "stocks", force: :cascade do |t|
 t.integer  "altas"
 t.integer  "bajas"
 t.integer  "items_id"
 t.datetime "created_at", null: false
 t.datetime "updated_at", null: false
end

add_index "stocks", ["items_id"], name: "index_stocks_on_items_id"

create_table "users", force: :cascade do |t|
 t.string   "name",                               null: false
 t.string   "email",                              null: false
 t.string   "encrypted_password",                 null: false
 t.string   "reset_password_token"
 t.datetime "reset_password_sent_at"
 t.datetime "remember_created_at"
 t.integer  "sign_in_count",          default: 0
 t.datetime "current_sign_in_at"
 t.datetime "last_sign_in_at"
 t.string   "current_sign_in_ip"
 t.string   "last_sign_in_ip"
 t.datetime "created_at"
 t.datetime "updated_at"
 t.integer  "role"
end

add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["reset_password_token"], name:     "index_users_on_reset_password_token", unique: true

end

Upvotes: 2

Views: 717

Answers (2)

frenciaj
frenciaj

Reputation: 128

Ok, here is how i solved it, so it may help in the future

here is the forms file:

<div class="panel-body">
  <div class="row">
    <table class="table table-hover">
      <thead class="warning">
       <th><center>Color</center></th>
       <th><center>Material</center></th>
       <th><center>Espesor</center></th>
       <th><center>Altas</center></th>
       <th ><center>Bajas</center></th>
      </thead>
   <%= form_tag stocks_path  do %>

   <% @items.each do |item| %>
   <%= fields_for 'data[]', @stock do |stock| %>
   <tbody>
  <tr>
  <td><%= item.nombre %></td>
  <td><center><%= item.material %></center></td>
  <td><center><%= item.espesor %></center></td>
  <td><center><%= stock.text_field :altas %></center></td>
  <td><center><%= stock.text_field :bajas %></center></td>
  <%= stock.hidden_field :item_id, :value => item.id %>
 <% end %>

<% end %>
</tr>
</tbody>
</table>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-5">
<%= submit_tag "Update", class: "btn btn-primary" %>
<% end %>
</div>
</div>

And this is how the controller is working

  @stock = Stock.create(my_params(d))
   if @stock.altas == nil && @stock.bajas == nil
   @stock.destroy
   else
   @stock.save

   end
 @item = Item.find(@stock.item_id)
  if @stock.altas != nil
   @item.quantity =  @stock.item.quantity + @stock.altas
  end
  if @stock.bajas != nil
   @item.quantity = @stock.item.quantity - @stock.bajas
  end
  @item.save
 end

  redirect_to stocks_url, notice: 'Stock was successfully created.'

end

Everything works, and its also substracting and adding to the total quantity

Upvotes: 1

BenKoshy
BenKoshy

Reputation: 35575

How to fix unknown attribute error

unknown attribute 'item_id' for Stock.

This basically means rails is looking for an "item_id" column in the stocks table. Add that in and it should work. You would have to rename the column with a migration.

Upvotes: 0

Related Questions