问题
I set up the following badge which registered in DB:
merit.rb
Merit::Badge.create!({
id: 1,
name: 'Five Tasks'
custom_fields: { img_url: '/images/badge.gif' }
})
I set up the following rule:
badge_rules.rb
grant_on 'tasks#create', :badge => 'Five Tasks', :temporary => true, :model_name => 'Task' do |task|
task.user_ids.count == 5
end
What I wanted to to happen is when the User created 5 tasks they would be issued a badge.
I see entries in the DB in merit_actions and merit_activity_log entries -- but I do not see anything in the badges_sashes which I assume is where they would show when the badge has been issued -- even though I have surpassed the threshold --
Also I am uncertain how to display the badges --- I am using:
<%= current_user.badges %>
Right now all I get is brackets -- what I want it to return is an image of the badge ---
Any assistance greatly appreciated.
回答1:
Are you accessing the badge's custom_fields as a hash? Those custom attributes that you add into custom_fields like img_url, difficulty, color, etc... won't be accessible in simple dot notation ie. badge.img_url
won't work. Instead you need to do something like badge.custom_fields[:img_url]
or badge.custom_fields[:whatever_custom_attribute]
. Hopefully, the fix will be that simple.
回答2:
To anyone else with a similar question: I finally was able to display the badges using the following:
<% current_user.badges.each do |badge| %>
<%= image_tag (badge.image) %>
<% end %>
回答3:
Update Rails 5
You have to specify the custom field param in the view :
/merit.rb
Merit::Badge.create!(
id: 2,
name: "FirstComment",
description: "Premier commentaire !",
custom_fields: { category: 'activity', image: 'ITM_02.svg' }
)
/view
<% @profil.badges.each do |badge| %>
<%= image_tag (badge.custom_fields[:image]) %>
<% end %>
来源:https://stackoverflow.com/questions/20511727/rails-merit-gem-badge-not-registering-or-displaying