【问题标题】:Trouble saving from console due to current_user NoMethodError由于 current_user NoMethodError,无法从控制台保存
【发布时间】:2014-07-14 19:10:53
【问题描述】:

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

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

【问题讨论】:

  • 请发布您的任务模型的代码

标签: ruby-on-rails devise console


【解决方案1】:

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

PublicActivity.enabled = false

如果您在启动控制台后运行该命令,您应该能够在不受 public_activity 干扰的情况下与模型进行交互。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 2014-10-24
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多