Rails 为 #<Merit::BadgeRules:0x0000000415b5e0> 分配未定义的局部变量或方法“user”
Rails Merit undefined local variable or method `user' for #<Merit::BadgeRules:0x0000000415b5e0>
我正在尝试使用 merti gem 与 rails 建立一个声誉系统,我试图在用户创建超过 8 条评论时授予徽章,但是我每次都会收到错误未定义方法用户我创建评论。我正在使用 devise ,但我可以手动创建徽章。有人可以帮忙吗?谢谢!!
徽章规则
# Be sure to restart your server when you modify this file.
#
# +grant_on+ accepts:
# * Nothing (always grants)
# * A block which evaluates to boolean (recieves the object as parameter)
# * A block with a hash composed of methods to run on the target object with
# expected values (+votes: 5+ for instance).
#
# +grant_on+ can have a +:to+ method name, which called over the target object
module Merit
class BadgeRules
include Merit::BadgeRulesMethods
def initialize
grant_on 'comments#create', badge: 'Jr.Critic', temporary: true, to: :user do |comment|
comment.user.comments.count >= 1 && comment.user.comments.count < 2
end
grant_on 'comments#create', badge: 'Sr.Critic', to: :user do |comment|
comment.user.comments.count >= 3
end
grant_on 'notes#create', badge: 'First story ', to: :user do |note|
note.user.notes.count = 1
end
grant_on 'comments#create', badge: 'Story Teller', to: :user do |comment|
comment.user.comments.count >= 8
end
merit.rb
# Use this hook to configure merit parameters
Merit.setup do |config|
Merit::Badge.create!(
id: 1,
name: "Jr.Critic",
description: "Over 5 comments"
)
Merit::Badge.create!(
id: 2,
name: "Sr.Critic",
description: "Over 50 comments"
)
Merit::Badge.create!(
id: 3,
name: "Story Teller",
description: "Over 5 notes!"
)
Merit::Badge.create!(
id: 4,
name: "First story ",
description: "created first story"
)
Merit::Badge.create!(
id: 5,
name: "commenter ",
description: "created more than 5 comments!"
)
和用户模型
class User < ApplicationRecord
has_merit
end
您似乎没有为用户和评论设置关联
User.rb应该还有,随着has_merit,has_many :comment
class User < ApplicationRecord
has_merit
has_many :comments
end
Comment.rb 应该有 belongs_to :user
class Comment < ApplicationRecord
belongs_to :user
end
此外,如果您还没有设置笔记的关联,请不要忘记。
您需要 运行 迁移以获得价值评论。然后重启你的服务器
并更改此行
grant_on 'notes#create', badge: 'First story ', to: :user do |note|
note.user.notes.count = 1
end
至
grant_on 'notes#create', badge: 'First story ', to: :user do |note|
note.user.notes.count = >1
end
希望对你有用
我正在尝试使用 merti gem 与 rails 建立一个声誉系统,我试图在用户创建超过 8 条评论时授予徽章,但是我每次都会收到错误未定义方法用户我创建评论。我正在使用 devise ,但我可以手动创建徽章。有人可以帮忙吗?谢谢!!
徽章规则
# Be sure to restart your server when you modify this file.
#
# +grant_on+ accepts:
# * Nothing (always grants)
# * A block which evaluates to boolean (recieves the object as parameter)
# * A block with a hash composed of methods to run on the target object with
# expected values (+votes: 5+ for instance).
#
# +grant_on+ can have a +:to+ method name, which called over the target object
module Merit
class BadgeRules
include Merit::BadgeRulesMethods
def initialize
grant_on 'comments#create', badge: 'Jr.Critic', temporary: true, to: :user do |comment|
comment.user.comments.count >= 1 && comment.user.comments.count < 2
end
grant_on 'comments#create', badge: 'Sr.Critic', to: :user do |comment|
comment.user.comments.count >= 3
end
grant_on 'notes#create', badge: 'First story ', to: :user do |note|
note.user.notes.count = 1
end
grant_on 'comments#create', badge: 'Story Teller', to: :user do |comment|
comment.user.comments.count >= 8
end
merit.rb
# Use this hook to configure merit parameters
Merit.setup do |config|
Merit::Badge.create!(
id: 1,
name: "Jr.Critic",
description: "Over 5 comments"
)
Merit::Badge.create!(
id: 2,
name: "Sr.Critic",
description: "Over 50 comments"
)
Merit::Badge.create!(
id: 3,
name: "Story Teller",
description: "Over 5 notes!"
)
Merit::Badge.create!(
id: 4,
name: "First story ",
description: "created first story"
)
Merit::Badge.create!(
id: 5,
name: "commenter ",
description: "created more than 5 comments!"
)
和用户模型
class User < ApplicationRecord
has_merit
end
您似乎没有为用户和评论设置关联
User.rb应该还有,随着has_merit,has_many :comment
class User < ApplicationRecord
has_merit
has_many :comments
end
Comment.rb 应该有 belongs_to :user
class Comment < ApplicationRecord
belongs_to :user
end
此外,如果您还没有设置笔记的关联,请不要忘记。
您需要 运行 迁移以获得价值评论。然后重启你的服务器 并更改此行
grant_on 'notes#create', badge: 'First story ', to: :user do |note|
note.user.notes.count = 1
end
至
grant_on 'notes#create', badge: 'First story ', to: :user do |note|
note.user.notes.count = >1
end
希望对你有用