Git Product home page Git Product logo

Comments (14)

tute avatar tute commented on August 21, 2024

Do you have something like:

Badge.create(
  :id => 100,
  :name => 'autobiographer',
  :description => ''
)

in your config/initializer/merit.rb file? If not, that's why and I'll create a more meaningul error message; if yes, it's a bug.

Thanks for reporting!

from merit.

tute avatar tute commented on August 21, 2024

Added to 'master' a better error message for if the error comes from a not found (not defined) badge, @jodyalbritton.

You may try it with gem 'merit', :git => 'git://github.com/tute/merit.git'

If the error persists, please tell me exactly how may I replicate it.

Best!

from merit.

jodyalbritton avatar jodyalbritton commented on August 21, 2024

I tried the version you specified, as well as double checking my config. As you can see points are being granted, but the user is not being found to assign a badge to.


MeritAction Load (0.4ms)  SELECT `merit_actions`.* FROM `merit_actions` WHERE `merit_actions`.`processed` = 0
  User Load (0.1ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
  Sash Load (0.1ms)  SELECT `sashes`.* FROM `sashes` WHERE `sashes`.`id` = 2 LIMIT 1
  User Load (0.2ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` IS NULL LIMIT 1
[merit] no target_obj found on Rule#applies?
  BadgesSash Load (0.1ms)  SELECT `badges_sashes`.* FROM `badges_sashes` WHERE `badges_sashes`.`sash_id` = 2
  CACHE (0.0ms)  SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1
   (0.1ms)  BEGIN
   (0.3ms)  UPDATE `users` SET `points` = 150, `updated_at` = '2012-06-11 07:59:51' WHERE `users`.`id` = 1
   (30.4ms)  COMMIT
   (0.1ms)  BEGIN
   (0.3ms)  UPDATE `merit_actions` SET `log` = 'points_granted:10|', `updated_at` = '2012-06-11 07:59:51' WHERE `merit_actions`.`id` = 111
   (31.1ms)  COMMIT
   (0.1ms)  BEGIN
   (0.3ms)  UPDATE `merit_actions` SET `processed` = 1, `updated_at` = '2012-06-11 07:59:51' WHERE `merit_actions`.`id` = 111
   (31.5ms)  COMMIT
  MeritAction Load (0.3ms)  SELECT `merit_actions`.* FROM `merit_actions` WHERE `merit_actions`.`id` = 111 LIMIT 1
Completed 302 Found in 454ms (ActiveRecord: 192.5ms)

from merit.

tute avatar tute commented on August 21, 2024

Maybe you already fixed it, but you have a typo in registrationss#create (an extra 's').

Now I see, you may be using devise. Do you have the registrations controller inside of your app? If not, Devise's controller don't set an instance variable in that controller, you have to set it yourself (for example, with @registration = build_resource). The target_object is not finding is not the user to be badged, but the object over which the action is applied (could be any entry, in this case it expects @registration.

I may add a wiki entry for this, or maybe we can come with a better solution for merit.

If you have the controller inside your app please copy it here, if not just override devise's. Thanks for reporting!

from merit.

tute avatar tute commented on August 21, 2024

May we change the issue's title, as the problem seems to have changed?

from merit.

jodyalbritton avatar jodyalbritton commented on August 21, 2024

Yes I think the typo was added during the paste, it was not a typo in my code. Second I am using devise, I am not sure I understand about copy the controller. It's fine to change the title.

from merit.

tute avatar tute commented on August 21, 2024

Please try this:

# Rule without ':model_name' option
grant_on ['registrations#create', 'registrations#update'], :badge => 'autobiographer', :temporary => true do |user|
  user.name.present? && user.username.present?
end

And add a file app/controllers/users/registrations_controller.rb:

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    @registration = build_resource # Needed for Merit
    super
  end

  def update
    @registration = resource # Needed for Merit
    super
  end
end

Merit needs an instance variable called like the singularized controller name, or like the :model_name option, to save it's id and find the meritable object. If it works correctly we'll add this to the wiki.

from merit.

jodyalbritton avatar jodyalbritton commented on August 21, 2024

Without defining a model I get uninitialized constant Registration

from merit.

tute avatar tute commented on August 21, 2024

Sorry, made a mistake, it's with model_name option:

grant_on ['registrations#create', 'registrations#update'], :badge => 'autobiographer', :model_name => 'User', :temporary => true do |user|
  user.name.present? && user.username.present?
end

And add a file app/controllers/users/registrations_controller.rb:

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    @user = build_resource # Needed for Merit
    super
  end

  def update
    @user = resource # Needed for Merit
    super
  end
end

from merit.

jodyalbritton avatar jodyalbritton commented on August 21, 2024

Seems like progress. I do not get the same error anymore. It still does not grant a badge however.

from merit.

tute avatar tute commented on August 21, 2024

Forgot to tell devise we are overriding it's controllers. In config/routes.rb (in the devise_for call):

devise_for :users, :controllers => { :registrations => 'users/registrations' }

from merit.

jodyalbritton avatar jodyalbritton commented on August 21, 2024

Okay looks like you solved it! Note the changes I made to the registrations controller class, no User required. Thanks for seeing this through Tute.

Here are my notes.

placed the following in controllers/registrations_controller.rb

class  RegistrationsController < Devise:: RegistrationsController
  def create
    @registration = build_resource # Needed for Merit
    super
  end

  def update
    @registration = resource # Needed for Merit
    super
  end
end

Placed the following in routes.rb

 devise_for :users, :controllers => { :registrations => 'registrations' }

Placed the following in models/merit/badge_rules.rb

 grant_on ['registrations#create', 'registrations#update'], :badge => 'autobiographer', :model_name => 'User' do |user|
            user.username.length > 2
      end

placed the following in initializers/merit.rb

Badge.create!({
   :id => 1,
   :name => 'autobiographer'
 })

from merit.

tute avatar tute commented on August 21, 2024

Nice, thank you for the feedback! If you want to add it to the wiki just do it, if not I'll do it in a little while.

from merit.

tute avatar tute commented on August 21, 2024

Wrapped up in https://github.com/tute/merit/wiki/How-to-grant-badges-on-user-registration-using-Devise

from merit.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.