undefined method 'new_record?' for nil: NilClass

0

I'm using the Cocoon gem and it gives me the following error

  

undefined method 'new_record?' for nil: NilClass

the situation is as follows. I have an Event ( event ) which has many users ( user ) and to relate them I have the model events_user .

event.rb

class Event < ApplicationRecord
    belongs_to :type_event
    belongs_to :contact
    has_many :events_user
    has_many :users, through: :events_user
    accepts_nested_attributes_for :events_user, :allow_destroy => true
end

user.rb

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :events_user
  has_many :events, through: :events_user
end

events_user.rb

class EventsUser < ApplicationRecord
    belongs_to :event
    belongs_to :user
end

_form.html.erb

....
<hr>
      <!-- render users -->
      <div class="users">
        <%= form.fields_for :events_users do |user| %>
          <%= render 'user_fields', f: user %>
        <%end%>
        <%= link_to_add_association "+", form, :users, class: "btn btn-primary" %>
      </div>

....

_user_fields.html.erb

<div class="form-row nested-fields">

    <div class="form-group col">
      <%= f.label :user_id, "Usuario" %>
      <%= f.collection_select(:user_id, User.all, :id, :name, {prompt: 'Selecciona un usuario'}, {class: "form-control"}) %>  
    </div>  

    <div class="form-group col">
      <%= f.label :estatus, "estatus" %>
      <%= f.select(:estatus, [['Notificado', 'Notificado'], ['Aceptado', 'Aceptado'], ['Rechazado', 'Rechazado']], {}, {class: "form-control"}) %>
    </div> 

    <div class="form-group col">
      <%= link_to_remove_association "-", f, class: "btn btn-danger" %>
    </div>

</div>

What I'm looking for is to insert in the Join table ( evets_user ) because I have more fields besides the event and the user. It is worth mentioning that users have been previously captured

UPDATE

Log

  

Started GET "/ events / new" for 127.0.0.1 at 2018-03-21 08:58:49 -0600   Processing by EventsController # new as HTML [1m [36mUser Load   (1.0ms) [0m [1m [34mSELECT users . * FROM users WHERE    users . id = 1 ORDER BY users . id ASC LIMIT 1 [0m Rendering   events / new.html.erb within layouts / application [1m [36mTypeEvent   Load (1.0ms) [0m [1m [34mSELECT type_events . * FROM    type_events [0m [1m [36mContact Load (0.0ms) [0m [1m [34mSELECT    contacts . * FROM contacts [0m [1m [36mUser Load (0.0ms) [0m   [1m [34mSELECT users . * FROM users [0m Rendered   events / _user_fields.html.erb (142.0ms) Rendered   events / _form.html.erb (202.0ms) Rendered events / new.html.erb within   layouts / application (249.0ms) Completed 500 Internal Server Error in   552ms (ActiveRecord: 2.0ms)

     

ActionView :: Template :: Error (undefined method new_record?' for nil:NilClass): 11: </div> 12: 13: <div class="form-group col"> 14: <%= link_to_remove_association "-", f, class: "btn btn-danger" %> 15: </div> 16:
17: </div> app/views/events/_user_fields.html.erb:14:in
_app_views_events__user_fields_html_erb

asked by Arturo Sustaita Castro 21.03.2018 в 18:19
source

1 answer

0

I think you should try creating an instance of the object you need to be rendered so no error should be generated, something like EventUser.new or EventUsert.build

    
answered by 30.03.2018 в 19:13