Rails Association issue with NoMethodError in event_controller

Posted by pmanning on Stack Overflow See other posts from Stack Overflow or by pmanning
Published on 2012-09-12T03:36:36Z Indexed on 2012/09/12 3:37 UTC
Read the original article Hit count: 210

Kinda a noob trying to understand I think I need to define rsvps but not sure what to put...

I'm trying to add a Join/Unjoin button to user created Events, similar to a Follow/Unfollow button for Users.

NoMethodError in Events#show undefined method `model_name' for NilClass:Class

in line #1 _unjoin.html.erb

1: <%= form_for(current_user.rsvps.find_by_joined_id(@event),
2:              html: { method: :delete }) do |f| %>
3:   <%= f.submit "Leave", class: "btn btn-large" %>
4: <% end %>

events_controller.rb

def show
    @event = Event.find(params[:id])
    @user = current_user
end

Here's the models

rsvp.rb

class Rsvp < ActiveRecord::Base
  attr_accessible :joined_id

  belongs_to :joiner, class_name: "User"
  belongs_to :joined, class_name: "User"

  validates :joiner_id, presence: true
  validates :joined_id, presence: true
end

user.rb

  has_many :rsvps, foreign_key: "joiner_id", dependent: :destroy
  has_many :joined_events, through: :rsvps, source: :joined
  has_many :reverse_rsvps, foreign_key: "joined_id",
                        class_name: "Rsvp",
                        dependent: :destroy
  has_many :joiners, through: :reverse_rsvps, source: :joiner

event.rb

  belongs_to :user

  has_many :rsvps, foreign_key: "joiner_id", dependent: :destroy
  has_many :joined_events, through: :rsvps, source: :joined
  has_many :reverse_rsvps, foreign_key: "joined_id",
                        class_name: "Rsvp",
                        dependent: :destroy
  has_many :joiners, through: :reverse_rsvps, source: :joiner

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about associations