由于current_user NoMethodError导致无法从控制台保存

时间:2014-07-14 19:10:53

标签: ruby-on-rails devise console

我一直试图通过控制台保存一些信息。我能够更新条目,但是当我继续尝试保存它时,我收到以下错误:

NoMethodError: undefined method `current_user' for nil:NilClass

我最初的反应是我需要"登录"对于用户,但控制台不应该要求这个(除非我错了)。我目前在我的本地服务器上仍然遇到此问题。我不久前能够完成这种动作,但现在它不允许我。这是一个很大的问题,因为我需要使用控制台手动更新条目以进行管理和/或故障排除。

仅提供我当前架构的示例(可能有用也可能没有帮助/相关):

2.1.1 :022 > task = Task.find(16)
  Task Load (0.2ms)  SELECT  "tasks".* FROM "tasks"  WHERE "tasks"."id" = ?  ORDER BY importance DESC, priority DESC, duedate, updated_at LIMIT 1  [["id", 16]]
 => #<Task id: 16, description: "new tasks", done: false, project_id: 8, created_at: nil, updated_at: nil, due: nil, started: true, active: nil, repeat: nil, time_limit: nil, priority: nil, assignee: "", creator: "sally@google.com", started_at: nil, completed_at: nil, paused_at: nil, resumed_at: nil, time_spent: nil, accepted: nil, duedate: nil, duetime: nil, repeat_time: nil, repeat_day: nil, repeat_date: nil, repeat_ordinal: nil, importance: ""> 
2.1.1 :023 > task.started_at = DateTime.now
 => Mon, 14 Jul 2014 12:02:47 -0700 
2.1.1 :024 > task.save
   (0.8ms)  begin transaction
  SQL (1.3ms)  UPDATE "tasks" SET "started_at" = ?, "updated_at" = ? WHERE "tasks"."id" = 16  [["started_at", "2014-07-14 12:02:47.458736"], ["updated_at", "2014-07-14 12:02:49.949408"]]
   (1.1ms)  rollback transaction
NoMethodError: undefined method `current_user' for nil:NilClass

我感谢任何人都能提供的帮助。提前谢谢。

任务模型:

class Task < ActiveRecord::Base
  belongs_to :project

  validates :description, presence: true

  default_scope { order('importance DESC', 'priority DESC', 'duedate','updated_at') }

  include PublicActivity::Model
  tracked owner: Proc.new{ |controller, model| controller.current_user }
end

1 个答案:

答案 0 :(得分:2)

根据the docs的public_activity gem,您可以使用此命令禁用跟踪:

PublicActivity.enabled = false

如果在启动控制台后运行该命令,则应该能够与模型进行交互而不会影响public_activity。