我已阅读有关 Merit gem 的所有当前问题和答案。并尝试遵循此处给出的解决方案Rails: Merit Gem Badge Not Registering or Displaying
初始化程序/merit.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 :mongo_mapper and :mongoid
config.orm = :active_record
# 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 $
config.current_user_method = "current_user"
end
# Create application badges (uses https://github.com/norman/ambry)
# Merit::Badge.create!(
badges = [
{ id: 1, name: 'democratic-society', description: "Voted twice", image: "straight.jpg" }
]
badges.each do |badge|
Merit::Badge.create!(badge)
end
但是经过3个小时的尝试,我一无所获。我在 sashes 中列出了一个徽章,并且我的用户有那个 sash id(badges_sashes 虽然返回“空集”......)但是当我尝试以下代码时:
应用程序.html.erb:
<% current_user.badges.each do |badge| %>
<%= badge.name %>
<%= badge.description %>
<%= image_tag (badge.image) %>
<% end %>
徽章规则.rb:
grant_on 'posts#vote_up', :badge => 'democratic-society', :to => :user do |post|
post.votes
end
没有任何错误消息......所以我对下一步该做什么有点茫然。我什至无法让它显示徽章名称。任何帮助,将不胜感激。我完全看不到任何东西。
post_controller.rb:
class PostsController < InheritedResources::Base
before_filter :authenticate_user!, only: [:vote_up]
def index
if params[:query].present?
@posts = Post.tire.search(:query => params[:query])
else
@posts = Post.order("created_at desc").paginate(:page => params[:page], :per_page => 15)
end
end
def vote_up
begin
current_user.vote_for(@post = Post.find(params[:id]))
flash[:success] = "Thanks for voting! Be sure to come back to find out the winners!"
redirect_to [@post]
rescue ActiveRecord::RecordInvalid
flash[:error] = "You have already voted on this one. You're only allowed one vote per song."
redirect_to [@post]
end
end
end
开发日志:
Processing by PostsController#vote_up as HTML
Parameters: {"authenticity_token"=>"SVWawkgb2hpkAJztuJjIKuIfRww/OjSAZAVpUqKeMOc=", "id"=>"243-worse-than-moe"}
User Load (1.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 5 LIMIT 1
Post Load (1034.1ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 243 LIMIT 1
EXPLAIN (35.6ms) EXPLAIN SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 243 LIMIT 1
EXPLAIN for: SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 243 LIMIT 1
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
| 1 | SIMPLE | posts | const | PRIMARY | PRIMARY | 4 | const | 1 | |
+----+-------------+-------+-------+---------------+---------+---------+-------+------+-------+
1 row in set (0.04 sec)
(0.1ms) BEGIN
Vote Exists (99.0ms) SELECT 1 AS one FROM `votes` WHERE (`votes`.`voteable_id` = BINARY 243 AND `votes`.`voteable_type` = 'Post' AND `votes`.`voter_type` = 'User' AND `votes`.`voter_id` = 5) LIMIT 1
SQL (152.3ms) INSERT INTO `votes` (`created_at`, `updated_at`, `vote`, `voteable_id`, `voteable_type`, `voter_id`, `voter_type`) VALUES ('2014-01-09 15:23:45', '2014-01-09 15:23:45', 1, 243, 'Post', 5, 'User')
(390.5ms) COMMIT
Redirected to http://liquid-radio.com:3000/posts/243-worse-than-moe
(0.2ms) BEGIN
SQL (47.5ms) INSERT INTO `merit_actions` (`action_method`, `action_value`, `created_at`, `had_errors`, `processed`, `target_id`, `target_model`, `updated_at`, `user_id`) VALUES ('vote_up', NULL, '2014-01-09 15:23:47', 0, 0, 243, 'posts',
'2014-01-09 15:23:47', 5)
(76.0ms) COMMIT
Merit::Action Load (0.3ms) SELECT `merit_actions`.* FROM `merit_actions` WHERE `merit_actions`.`processed` = 0
(0.1ms) BEGIN
(160.6ms) UPDATE `merit_actions` SET `processed` = 1, `updated_at` = '2014-01-09 15:23:47' WHERE `merit_actions`.`id` = 13
(99.3ms) COMMIT
Post Load (0.1ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 243 LIMIT 1
CACHE (0.0ms) SELECT `posts`.* FROM `posts` WHERE `posts`.`id` = 243 LIMIT 1
Completed 302 Found in 7214.0ms (ActiveRecord: 2337.7ms)