0

我正在尝试使用 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

优点.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
4

2 回答 2

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

此外,如果您没有设置注释关联,请不要忘记设置关联。

于 2017-05-27T16:05:55.277 回答
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

希望这对你有用

于 2017-05-27T16:20:12.740 回答