Rails NameError 'uninitialized constand User::Events' 使用设计

Rails NameError 'uninitialized constand User::Events' using devise

我觉得标题可以更具体一些,但我很难理解这个问题,而且我不确定问题出在哪里,所以我深表歉意。

我有使用 devise 创建的用户,用户可以创建事件,并且这些事件有许多用户作为参与者与他们相关联。

我的用户模型:

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

  has_many :created_events, foreign_key: :owner_id, class_name: "Events"
  has_many :attendees, through: :attendees

end

我的活动模型

class Event < ApplicationRecord
  belongs_to :owner, class_name: 'User'
  has_many :attendees, through: :attendees
end

我的attendee模型,加入两个

class Attendee < ApplicationRecord
  belongs_to :attendee, class_name: "User"
  belongs_to :attended_event, class_name: "Event"
end

在我的 users_controller 文件 users_controller 中尝试使用这样的东西时,我得到了“未初始化的常量 User::Events” 错误,转到 /users/[:id]:

class UsersController < ApplicationController

  before_action :authenticate_user!

  def show
    @user = User.find(params[:id])
    @created_events = @user.created_events.all
  end

end

或者在 events_controller 文件中执行新 > 创建操作时:

def new
  @event = Event.new
end

def create
  @event = current_user.created_events.build(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, notice: 'Event was successfully created.' }
        format.json { render :show, status: :created, location: @event }
      else
        format.html { render :new }
        format.json { render json: @event.errors, status: :unprocessable_entity }
    end
  end
end

在我添加连接模型之前它工作正常,现在我收到错误:

uninitialized constant User::Events

提前致谢。我真的很难过。

简单的错字:

has_many :created_events, foreign_key: :owner_id, class_name: "Events"

应该是class_name: 'Event'