我试图使用merti gem建立一个使用rails的信誉系统,我试图在用户创建超过8条评论时授予徽章但是我每次创建评论时都会获得错误的未定义方法用户。我使用设计,但我可以手动创建徽章。有人可以帮忙吗?谢谢!!
徽章规则
# 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
答案 0 :(得分:1)
看起来您没有为用户和评论设置关联
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
另外,如果你不喜欢,也不要忘记设置笔记的关联。
答案 1 :(得分:0)
您需要针对优点评论进行迁移。然后重启你的服务器 并改变这一行
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
希望它适合你