Rails - 当删除一个Pain时,Current_user为nil

时间:2016-12-21 11:04:35

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

我正在使用公共活动来获取通知。我在祈祷模型中跟踪新的祈祷。出于某种原因,当我删除一个疼痛并且我不明白为什么时,current_user是nil。

var data = new FormData();
jQuery.each(jQuery('#file')[0].files, function(i, file) {
    data.append('file-'+i, file);
});

$("#commentForm").submit(function(e)
{
     e.preventDefault();
     $("#divLoading").addClass('show');

     $.ajax(
     {
        url : $(this).attr("action"),
        type: "POST",
        data: data,
        enctype: 'multipart/form-data',
        processData: false, 
        contentType:false,
     });
});

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

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

Edit2:Rails admin config

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

1 个答案:

答案 0 :(得分:0)

好的我找到了解决方案,在我的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

现在我可以在我的祷告模型中访问current_user。

感谢所有人