0

Using the merit gem, I want to create a Pioneer badge for the first 100 users of my app.

The code in merit.rb

Merit::Badge.create!(
  id: 1,
  name: 'Pioneer',
  description: "Belongs to the 100 first users of the site",
  image: '/images/pioneer.png'
)

The code from badges_rules.rb takes in consideration that i'm using devise for authentification. So I followed this how to.

grant_on 'users/registrations#create', badge: 'Pioneer', model_name: 'User' do |user|
  user.id < 101
end

It's not creating any badge. It's interesting to notice that this other badge is working very well :

grant_on 'users/registrations#create', badge: 'Inscription', model_name: 'User'

It seems that Devise is messing with the user object. I did override the registration controller, exactly like the Howto said. And when I call this controller in a simple way, like with this "Inscription" badge, everything's ok.

But when I need to put a condition on the user's id, nothing happens.

For information, this is the code from user's show view, where the badges are displayed (this is working).

 <% @user.badges.first(5).each do |b| %>
   <%= image_tag(b.image) %>
   <%= b.name %>
 <%end%>
Ruff9
  • 1,163
  • 15
  • 34

1 Answers1

0

Add model_name: 'User' to your rule. This happens when the controller name is not named after the object (registrations != user).

TuteC
  • 4,342
  • 30
  • 40
  • Thanks for your answer. But my user still have no badge :( I looked into your [demo app](https://github.com/tute/merit/blob/master/test/dummy/app/models/merit/badge_rules.rb) and tried to make an automatic badge : `grant_on 'registrations#create', badge: 'just-registered', model_name: 'User', to: :user do true end`. It looks simple, but it's not working, I'm missing something somewhere. – Ruff9 Mar 06 '14 at 09:31
  • I'm still digging for the simpliest possible badge, and I'm currently trying this one : `grant_on 'users#update', badge: 'profil_edite', to: :user` so, I got `undefined method _sash' for true:TrueClass` with this funny log line: `[merit] NoMethodError on User#user (called from Merit::TargetFinder#other_target)`. I think I'm getting close. – Ruff9 Mar 06 '14 at 11:47
  • I did it. And I found a mistake in your "How to grant badges on user registration using Devise". On the point 2, when you write `grant_on 'registrations#create'` in fact you mean `grant_on 'users/registrations#create'` – Ruff9 Mar 06 '14 at 14:13
  • And also, I don't think you mentioned the :image field in the create badge section – Ruff9 Mar 06 '14 at 14:25
  • sorry for the comments flood. Since the pioneer badge isn't working yet, I'm updating the whole question. – Ruff9 Mar 06 '14 at 19:29
  • `grant_on 'users#update', badge: 'profil_edite', to: :user` should be to itself: `grant_on 'users#update', badge: 'profil_edite', to: :itself`. Regarding `grant_on 'registrations#create'` or `grant_on 'users/registrations#create'` it depends on how do you override devise controllers in your app. – TuteC Mar 07 '14 at 05:05
  • `image` is not used anyware, thanks for noting, taking it out from attr_accessible now. You can define custom fields like that one in the `custom_fields` hash. – TuteC Mar 07 '14 at 05:07