问题
I've read all the current questions and answers about the Merit gem. And attempted to follow the solution given here Rails: Merit Gem Badge Not Registering or Displaying
initializers/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
But after 3 hours of trying I've gotten nowhere. I have a badge listed in sashes and my user has that sash id (badges_sashes returns "empty set" though...) but when I try the following code:
application.html.erb:
<% current_user.badges.each do |badge| %>
<%= badge.name %>
<%= badge.description %>
<%= image_tag (badge.image) %>
<% end %>
badge_rules.rb:
grant_on 'posts#vote_up', :badge => 'democratic-society', :to => :user do |post|
post.votes
end
There's no error message whatsoever... so I'm at a bit of a loss on what to do next. I can't even get it to display the badge name. Any help would be appreciated. I get absolutely nothing in the view.
posts_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
development.log:
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)
来源:https://stackoverflow.com/questions/21009038/rails-3-2-merit-gem-badge-not-working