Rails: Merit Gem Badge Not Registering or Displaying

Merit gem - Display a specific badge and the users who obtained it

I finally managed to make it work!

Here is my solution:

In badges_controller.rb:

class BadgesController < ApplicationController

# GET /badges/1
def show; end

private

# Set badge
def set_badge
@badge = Merit::Badge.find(params[:badge_id])
@id = @badge.keys[params[:id].to_i]
@badge = @badge.find(@id)
end

end

Then in the view show.html.erb, it is possible to call the usual methods as per the documentation:

<%= @badge.name %>
<%= @badge.description %>
<% @badge.users.each do |user| %>
<%= user.full_name %>
<%= (user.sash.badges_sashes[params[:id].to_i].created_at).to_formatted_s(:long) %>
<% end %>

For information, the path must be badge_path(badge.id - 1) to redirect to the correct badge (arrays start at 0).

How to display the creation date of a badge earning with merit gem

You've got to go through sash and badges_sashes relations:

 <% @profil.sash.badges_sashes.each do |badge_sash| %>
<%= image_tag(badge_sash.badge.custom_fields[:image]), badge_sash.created_at %>
<% end %>

You can read about merit internals in https://github.com/merit-gem/merit/wiki/General-merit-workflow.

rails merit gem does nothing?

Look at this, i think that can give you a way!

good luck!

Observable badge events using merit gem

The Observers are notified when the reputation (points, ranking or badges) are granted through the rules defined in app/models/merit. The Judge object takes care of this.

When you grant reputation "by hand" (calling add_badge) you need to call the observer manually, as in that sample code from Judge.

Rails 4 / Devise / Merit gem : no target_obj found on Rule#applies?

The :to parameter can be a method name, which called over the target object (in this case, @registration) should retrieve the object to badge. If using it this way, when you put :user, then merit is internally running @registration.user to find who to badge, which is not what we want.

:to can also be :itself, in that case it badges the target object (@registration in your case, which, if a User, should work for you).

I believe you want to badge whoever is current_user, which is this option's default (:action_user), so you shouldn't need to define :to option.

That said, for merit to badge, condition has to be true, and it can happen that there's no user to pass as a parameter to that rule. Try it without the parameter:

grant_on 'registrations#create', badge: 'Pioneer' do
User.count < 101
end


Related Topics



Leave a reply



Submit