class 被传递给 `:class_name` 但我们期待一个字符串
A class was passed to `:class_name` but we are expecting a string
我正在尝试创建一个名为 :books_users 的联接 table,其中 books 中的一列 :claim 是一个布尔值,如果有人单击 link "review this book",图书控制器中的声明操作是这样做的:
def claim
book = Book.find(params[:id])
book.claims << current_user unless book.claims.include?(current_user)
redirect_to current_user
flash[:notice] = "You have a new book to review!"
end
这样做的目的是让注册为评论者的我的用户可以进入图书展示页面,如果他们决定评论评论者通过类型找到的作者上传的图书?然后他们基本上表示他们要评论那本书,他们的评论最终将作为经过验证的购买评论出现在亚马逊上,而不是书籍展示页面上网站上的俗气文本评论(这将使注册的作者审稿服务很开心)。
我的模型是这样的:
book.rb
class Book < ApplicationRecord
mount_uploader :avatar, AvatarUploader
belongs_to :user
has_and_belongs_to_many :genres
has_and_belongs_to_many :claims, join_table: :books_users, association_foreign_key: :user_id
end
user.rb
class User < ApplicationRecord
mount_uploader :avatar, AvatarUploader
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :books
enum access_level: [:author, :reviewer]
has_and_belongs_to_many :claims, join_table: :books_users, association_foreign_key: :book_id
end
当评论者点击 link 来评论这本书时,我在 BooksController 中收到 NameError#claim
未初始化常量Book::Claim
我试图在命名 foreign_key_association 之后在模型中的 hmbtm 关系中指定,我做了一个 class_name: ClassName,认为这可能会解决错误,但我明白了一个新的说 A class 被传递给 :class_name
但我们期待一个字符串。
我真的很困惑,需要有人向我解释一下。谢谢!
错误提示您应该将字符串作为 class_name
参数传递,或者您可以使用未记录的 class
选项:
class Foo < AR
has_many :bars, class_name: to_s
# to_s returns the class name as string the same as Bar.class.to_s
end
或:
class Foo < AR
has_many :bars, class: Baz # returns the class
end
我正在尝试创建一个名为 :books_users 的联接 table,其中 books 中的一列 :claim 是一个布尔值,如果有人单击 link "review this book",图书控制器中的声明操作是这样做的:
def claim
book = Book.find(params[:id])
book.claims << current_user unless book.claims.include?(current_user)
redirect_to current_user
flash[:notice] = "You have a new book to review!"
end
这样做的目的是让注册为评论者的我的用户可以进入图书展示页面,如果他们决定评论评论者通过类型找到的作者上传的图书?然后他们基本上表示他们要评论那本书,他们的评论最终将作为经过验证的购买评论出现在亚马逊上,而不是书籍展示页面上网站上的俗气文本评论(这将使注册的作者审稿服务很开心)。
我的模型是这样的:
book.rb
class Book < ApplicationRecord
mount_uploader :avatar, AvatarUploader
belongs_to :user
has_and_belongs_to_many :genres
has_and_belongs_to_many :claims, join_table: :books_users, association_foreign_key: :user_id
end
user.rb
class User < ApplicationRecord
mount_uploader :avatar, AvatarUploader
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :books
enum access_level: [:author, :reviewer]
has_and_belongs_to_many :claims, join_table: :books_users, association_foreign_key: :book_id
end
当评论者点击 link 来评论这本书时,我在 BooksController 中收到 NameError#claim
未初始化常量Book::Claim
我试图在命名 foreign_key_association 之后在模型中的 hmbtm 关系中指定,我做了一个 class_name: ClassName,认为这可能会解决错误,但我明白了一个新的说 A class 被传递给 :class_name
但我们期待一个字符串。
我真的很困惑,需要有人向我解释一下。谢谢!
错误提示您应该将字符串作为 class_name
参数传递,或者您可以使用未记录的 class
选项:
class Foo < AR
has_many :bars, class_name: to_s
# to_s returns the class name as string the same as Bar.class.to_s
end
或:
class Foo < AR
has_many :bars, class: Baz # returns the class
end