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

时间:2018-08-09 05:43:28

标签: ruby-on-rails devise merit-gem

我有

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,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'

1 个答案:

答案 0 :(得分:0)

我可以和它一起工作

import java.time.*;
import java.util.regex.*;

public class MyClass {
    public static void main(String args[]) {
        LocalDate today = LocalDate.now();
        long storagePeriod = 5L;
        String fileName = "file_20180804";
        int year = 0;
        int month = 0;
        int day = 0;

        String pattern = "file_(\\d{4})(\\d{2})(\\d{2})";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(fileName);

        if (m.find()) {
            year = Integer.parseInt(m.group(1));
            month = Integer.parseInt(m.group(2));
            day = Integer.parseInt(m.group(3));
        }

        LocalDate fileDate = LocalDate.of(year, month, day);

        if (fileDate.isBefore(today.minusDays(storagePeriod))) {
            System.out.println("Delete this file");
        }
    }
}

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参数。

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

  

优点将从该更新操作中发送的:id参数找到的数据库中获取Article对象。