Rails: Merit Gem Badge Not Registering or Displaying

北慕城南 提交于 2019-12-01 14:27:07

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.

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 %>

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 %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!