我想使用 Merit gem 但是......它什么也没显示。我已经读过这个Rails: Merit Gem Badge Not Registering or Displaying和这个Rails 3.2 Merit Gem Badge not Working但我不明白为什么它不起作用。
优点.rb
# Use this hook to configure merit parameters
Merit.setup do |config|
# Check rules on each request or in background
# config.checks_on_each_request = true
# Define ORM. Could be :active_record (default) and :mongoid
config.orm = :active_record
# Add application observers to get notifications when reputation changes.
# config.add_observer 'MyObserverClassName'
# Define :user_model_name. This model will be used to grand badge if no
# `:to` option is given. Default is 'User'.
config.user_model_name = 'User'
# Define :current_user_method. Similar to previous option. It will be used
# to retrieve :user_model_name object if no `:to` option is given. Default
# is "current_#{user_model_name.downcase}".
config.current_user_method = 'current_user'
end
# Create application badges (uses https://github.com/norman/ambry)
badge_id = 0
[{
id: (badge_id = badge_id+1),
name: 'Timide',
description: '1 comment',
level: 1
}, {
id: (badge_id = badge_id+1),
name: 'Bavard',
description: '3 comments',
level: 3
},{
id: (badge_id = badge_id+1),
name: 'Pipelette',
description: '5 comments',
level: 5
}].each do |attrs|
Merit::Badge.create! attrs
end
徽章规则.rb
module Merit
class BadgeRules
include Merit::BadgeRulesMethods
def initialize
# If it creates user, grant badge
# Should be "current_user" after registration for badge to be granted.
# grant_on 'users#create', badge: 'just-registered', to: :itself
# If it has 1 comment, grant commenter-1 badge
grant_on 'comments#index', badge: 'Timide', level: 1 do |comment|
comment.user.comments.count == 1
end
评论控制器.rb
class CommentsController < ApplicationController
before_filter :authenticate_user!, except: [:index, :show]
# GET /comments
# GET /comments.json
def index
@comments = Comment.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @comments }
end
end
我使用 current_user.badges 在我的视图中显示。
谢谢你的帮助 !