我得到这个错误
NameError(未初始化的常量User::ChatRoom(:
app/controller/users_controller.rb:20:in"create">
这是我的user.rb文件
class User
include Mongoid::Document
field :name, type: String
field :email, type: String
has_many :chat_room, dependent: :destroy
end
这是我的聊天_room.rb文件
class ChatRoom
include Mongoid::Document
# Attributes
field :tittle, type: String
field :is_private, type: Boolean, default: false
# Relations
belongs_to :user
# Validations and Scope
scope :public_rooms, -> { where(is_private: false) }
end
users_controller.rb文件
def create
user = User.new(user_params)
if user.save
render json: { status: :created}
else
render json: user.errors, status: :unprocessable_entity
end
end
在您的类User中,更改:
has_many:chat_room,dependent::destroy
作者:
has_many:chat_rooms,dependent::destroy
在has_many
之后,它应该是多类名。并且在belongs_to
之后,它应该是单数类名。