Nested attributes with rails

2

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 .

    
asked by anonymous 17.05.2016 / 20:27

2 answers

2

Good morning!

Try changing this:

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

For this:

<h3>Items</h3>
  <div id="inventory_items">
    <%= f.simple_fields_for :inventory do |inventory| %>
      <%= inventory.simple_fields_for :inventory_items do |item| %>
        <%= render 'item_fields', f: item %>
      <% end %>
    <% end %>
  </div>

It may be that you are getting lost in f's because you are referencing different things.

I hope I have helped.

    
24.05.2016 / 17:32
0

Try to make the following change.

@user = User.new.tap
@user.build_inventory
@user.inventory.inventory_items.build

This other question helped me to clarify the use of build_ and .build. I recommend taking a look. link

    
15.08.2016 / 19:46