无法从设计会话控制器访问用户模型:[优点]在规则#上找不到target_obj?



我有

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" = $1  ORDER BY "users"."id" ASC LIMIT 1  [["email", "student@gmail.com"]]
   (7.0ms)  BEGIN
  SQL (3.0ms)  UPDATE "users" SET "last_sign_in_at" = $1, "current_sign_in_at" = $2, "sign_in_count" = $3, "updated_at" = $4 WHERE "users"."id" = $5  [["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" = $1 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 ($1, $2, $3, $4, $5, $6) 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" = $1  [["processed", "f"]]
   (3.0ms)  BEGIN
  SQL (2.0ms)  UPDATE "merit_actions" SET "processed" = $1, "updated_at" = $2 WHERE "merit_actions"."id" = $3  [["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,轨道 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 将从数据库中获取Article对象,该对象由在该更新操作中发送的:id参数找到。

最新更新