【问题标题】:Rails - Current_user is nil when delete a PainRails - 删除 Pain 时 Current_user 为零
【发布时间】:2016-12-21 11:04:35
【问题描述】:

我正在使用公共活动来获取通知。我正在我的祷告模型中跟踪新的祷告。出于某种原因,当我删除 Pain 时 current_user 为零,我不明白为什么。

class Prayer < ApplicationRecord
 include PublicActivity::Model

  has_many :activities, as: :trackable, class_name: 'PublicActivity::Activity', dependent: :destroy
  tracked owner: Proc.new { |controller, model| controller.current_user } #fail current_user is nil
end

class Pain < ApplicationRecord
 belongs_to :user
 has_many :prayers, dependent: :destroy
end

我可以提供更多代码,但我不确定什么会有用。 非常感谢

编辑:我忘了说我正在尝试删除 Rails 管理页面中的 Pain

Edit2:Rails 管理员配置

RailsAdmin.config do |config|

 config.actions do
  dashboard                     # mandatory
  index                         # mandatory
  new
  export
  bulk_delete
  show
  edit
  delete
  show_in_app
 end

 config.authorize_with do |controller|
  redirect_to main_app.root_path unless current_user &&    current_user.admin
 end
end

【问题讨论】:

  • 你没有登录,我猜?因此,没有当前用户。
  • 我忘了说我正在尝试删除 Rails 管理页面中的 Pain
  • 你能展示一下你的 Rails Admin 配置吗?
  • 好的,我已经编辑了我的帖子

标签: ruby-on-rails devise public-activity


【解决方案1】:

好的,我找到了解决方案,在我的 ApplicationController 中我调用了 helper_method :current_user

class ApplicationController < ActionController::Base
  helper_method :current_user

  def current_user
    @current_user ||= User.find_by(id: session[:user])
  end
end

现在我可以在我的 Prayer 模型中访问 c​​urrent_user。

谢谢大家

【讨论】:

    猜你喜欢
    • 2013-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多