0

我设置了以下在 DB 中注册的徽章:

优点.rb

Merit::Badge.create!({
id: 1,
name: 'Five Tasks'
custom_fields: { img_url: '/images/badge.gif' }
})

我设置了以下规则:

徽章规则.rb

grant_on 'tasks#create', :badge => 'Five Tasks', :temporary => true, :model_name => 'Task' do |task|
task.user_ids.count == 5
end

我想要发生的是当用户创建 5 个任务时,他们将获得一个徽章。

我在merit_actions 和merit_activity_log 条目中看到数据库中的条目——但我在badges_sashes 中看不到任何东西,我认为这是颁发徽章时它们会显示的地方——即使我已经超过了阈值——

我也不确定如何显示徽章——我正在使用:

<%= current_user.badges %>

现在我得到的只是括号——我希望它返回的是徽章的图像——

非常感谢任何帮助。

4

3 回答 3

2

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.

于 2014-03-12T07:08:20.557 回答
0

对于其他有类似问题的人:我终于能够使用以下方法显示徽章:

<% current_user.badges.each do |badge| %>
<%= image_tag (badge.image) %>
<% end %>
于 2013-12-13T04:05:45.917 回答
0

更新 Rails 5

您必须在视图中指定自定义字段参数:

/优点.rb

  Merit::Badge.create!(
   id: 2,
   name: "FirstComment",
   description: "Premier commentaire !",
   custom_fields: { category: 'activity', image: 'ITM_02.svg' }
  )

/看法

<% @profil.badges.each do |badge| %>                               
   <%= image_tag (badge.custom_fields[:image]) %>
<% end %>
于 2018-05-04T08:14:52.813 回答