ruby-on-railsdevisepublic-activity

Rails - Current_user is nil when delete a Pain


I'm using Public Activity to get notifications. I'm tracking the new prayers in my Prayer model. For some reason current_user is nil when i deleted a Pain and i don't understand why.

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

I can provide more code, but I'm not sure what would be useful. Thanks a lot

Edit: I forgot to say that i'm trying to delete the Pain in the Rails admin page

Edit2 : Rails admin config

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

Solution

  • Ok i found the solution, in my ApplicationController i call the 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
    

    Now i can access to current_user in my Prayer model.

    Thanks for all guys