I am developing a system for studying, which will allow the creation of a user. This user, in turn, will have an inventory, which will have several items.
My question is how to save all these resources at once when accessing the action new
of UsersController
.
I'm using cocoon
, and the error that appears is:
undefined method 'new_record?' for nil:NilClass
Models
class User < ActiveRecord::Base
has_one :inventory
delegate :items, to: :inventory, prefix: true
accepts_nested_attributes_for :inventory
end
class Inventory < ActiveRecord::Base
has_many :inventory_items
has_many :items, through: :inventory_items
accepts_nested_attributes_for :inventory_items, reject_if: :all_blank, allow_destroy: true
end
class InventoryItem < ActiveRecord::Base
belongs_to :item
belongs_to :inventory
end
Controller
class UsersController < ApplicationController
def new
@user = User.new.tap do |user|
user.inventory = Inventory.new
user.inventory_items.build
end
end
private
def user_params
params
.require(:user)
.permit(
:name,
:lat,
:long,
:age,
inventory_attributes: [:id, inventory_items_attributes: [:quantity, :inventory_id, :item_id, :_destroy]]
)
end
end
User registration form
<%= simple_form_for(@user) do |f| %>
<%= f.error_notification %>
<div class="form-inputs">
<%= f.input :name %>
<%= f.input :lat %>
<%= f.input :long %>
</div>
<h3>Items</h3>
<div id="inventory_items">
<%= f.simple_fields_for :inventory do |inventory| %>
<%= inventory.simple_fields_for :inventory_items do |f| %>
<%= render 'item_fields', f: f %>
<% end %>
<% end %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
Partial for items
<div class="nested-fields">
<%= f.collection_select :item_id, Item.all, :id, :name %>
<%= f.input :quantity %>
<%= link_to_remove_association "remove item", f %>
</div>
Note: The inventory has no attribute other than its id
and user_id
.