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
. * FROMusers
WHEREusers
.id
= 1 ORDER BYusers
.id
ASC LIMIT 1 [0m Rendering events / new.html.erb within layouts / application [1m [36mTypeEvent Load (1.0ms) [0m [1m [34mSELECTtype_events
. * FROMtype_events
[0m [1m [36mContact Load (0.0ms) [0m [1m [34mSELECTcontacts
. * FROMcontacts
[0m [1m [36mUser Load (0.0ms) [0m [1m [34mSELECTusers
. * FROMusers
[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:
_app_views_events__user_fields_html_erb
17: </div> app/views/events/_user_fields.html.erb:14:in