代码之家  ›  专栏  ›  技术社区  ›  Chloe

无法从“设计会话”控制器访问用户模型:[merit]在规则“应用”上找不到目标对象?

  •  0
  • Chloe  · 技术社区  · 6 年前

    我有

    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'
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Chloe    6 年前

    我有办法

    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

    功绩会使 Article 数据库中的对象,由 :id号 在更新操作中发送的参数。