无法从 Devise 会话控制器访问用户模型:[merit] 在 Rule#applies 上找不到 target_obj?

Cannot access user model from Devise sessions controller: [merit] no target_obj found on Rule#applies?

我有

class CustomSessionsController < Devise::SessionsController
  def create
    @user = resource # needed for Merit
    super
  end

  protected

    def after_sign_in_path_for(resource)
      @user = resource # needed for Merit
      resource.update_streak
      super

  grant_on 'custom_sessions#create', badge: :streak, level: 3, temporary: true, model_name: 'User' do |user|
    puts user.inspect
    user.streak.count >= 3
  end

但是报错

[merit] no target_obj found on Rule#applies?

我无法访问该模型,它不会授予徽章或记录用户。怎么了?我按照指南做了。

https://github.com/merit-gem/merit/wiki/How-to-grant-badges-on-user-using-Devise

它正在做某事。

Processing by CustomSessionsController#create as HTML
  Parameters: {"utf8"=>"√", "authenticity_token"=>"gqUQjF9hfzJdQqxAAQJxv7bi+kZYwuv1NWtOP0YhkbjHKwnfa5WAb/CkRZ5c+Xi5yVlnJ2v774w3XLhTa1b1sQ==", "user"=>{"email"=>"student@gmail.com", "password"=>"[FILTERED]", "remember_me"=>"0"}, "commit"=>"Log in"}
  User Load (6.0ms)  SELECT  "users".* FROM "users" WHERE "users"."email" =   ORDER BY "users"."id" ASC LIMIT 1  [["email", "student@gmail.com"]]
   (7.0ms)  BEGIN
  SQL (3.0ms)  UPDATE "users" SET "last_sign_in_at" = , "current_sign_in_at" = , "sign_in_count" = , "updated_at" =  WHERE "users"."id" =   [["last_sign_in_at", "2018-08-09 05:38:58.345271"], ["current_sign_in_at", "2018-08-10 01:40:51.644592"], ["sign_in_count", 15], ["updated_at", "2018-08-10 01:40:51.668609"], ["id", 3]]
   (25.0ms)  COMMIT
  Streak Load (21.0ms)  SELECT  "streaks".* FROM "streaks" WHERE "streaks"."user_id" =  LIMIT 1  [["user_id", 3]]
Redirected to http://localhost:3000/
   (1.0ms)  BEGIN
  SQL (7.0ms)  INSERT INTO "merit_actions" ("user_id", "action_method", "target_model", "target_data", "created_at", "updated_at") VALUES (, , , , , ) RETURNING "id"  [["user_id", 3], ["action_method", "create"], ["target_model", "custom_sessions"], ["target_data", "--- \n...\n"], ["created_at", "2018-08-10 01:40:53.539847"], ["updated_at", "2018-08-10 01:40:53.539847"]]
   (8.0ms)  COMMIT
  Merit::Action Load (6.0ms)  SELECT "merit_actions".* FROM "merit_actions" WHERE "merit_actions"."processed" =   [["processed", "f"]]
   (3.0ms)  BEGIN
  SQL (2.0ms)  UPDATE "merit_actions" SET "processed" = , "updated_at" =  WHERE "merit_actions"."id" =   [["processed", "t"], ["updated_at", "2018-08-10 01:40:53.581875"], ["id", 17]]
   (20.0ms)  COMMIT
  User Load (2.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
[merit] no target_obj found on Rule#applies?
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
[merit] no target_obj found on Rule#applies?
  CACHE (0.0ms)  SELECT  "users".* FROM "users" WHERE "users"."id" IS NULL LIMIT 1
Completed 302 Found in 2567ms (ActiveRecord: 293.2ms)

优点 2.4,Rails 4.2.


我试过了

  grant_on 'custom_sessions#create', badge: :streak, level: 3, temporary: true do 
    puts current_user.inspect
    current_user.streak.count >= 3
  end

但它给了

[merit] no target found: uninitialized constant CustomSession. base_target_finder.rb:13:in 'find' 
error NameError (undefined local variable or method 'current_user'

我试过了

  grant_on 'custom_sessions#create', badge: :streak, level: 3, temporary: true, to: :itself do |user|
    puts user.inspect
    user.streak.count >= 3
  end

  def create
    @custom_session = resource # needed for Merit

  def after_sign_in_path_for(resource)
    @custom_session = resource # needed for Merit

但它给了

[merit] no target found: uninitialized constant CustomSession. C:/ruby23/lib/ruby/gems/2.3.0/gems/merit-2.4.0/lib/merit/base_target_finder.rb:13:in `find'
true
Completed 500 Internal Server Error in 2181ms (ActiveRecord: 177.1ms)

NoMethodError (undefined method `streak' for true:TrueClass):
  app/models/merit/badge_rules.rb:43:in `block in initialize'

我得到它与

一起工作
grant_on 'custom_sessions#create', badge: :streak, level: 3, temporary: true, model_name: 'User', to: :itself do |user|

  def create
    super
    @custom_session = resource # needed for Merit

但我不知道为什么,因为 /users/sign_in 路径没有 :id 参数。

https://github.com/merit-gem/merit#how-merit-finds-the-target-object

Merit would fetch the Article object from the database, found by the :id param sent in that update action.