简体   繁体   中英

Cant acess current_user on datagrid rails

I'm having a problem trying to access the current_user attr_acessor in a datagrid (gem), I pass the current_user through the controller (I'll post the codes below) and then I should access it in the datagrid, so much so that in one of the methods ( show_executivos) I can access it to make a conditional, however in the rest of the class and mainly within the filters I cannot access it, thanks!

users_controller.rb:

def users_parceiro

  authorize User

  if params[:users_parceiro_grid].nil? || params[:users_parceiro_grid].blank?
    @users = UsersParceiroGrid.new(:current_user => @current_user)
  else
    @users = UsersParceiroGrid.new(params.fetch(:users_parceiro_grid, {}).merge(current_user: @current_user))
  end
end

users_parceiro_grid.rb:

class UsersParceiroGrid

  include Datagrid

  Datagrid.configure do |config|
    config.date_formats = ["%d/%m/%Y", "%d-%m-%Y"]
    config.datetime_formats = ["%d/%m/%Y %h:%M", "%d-%m-%Y %h:%M:%s"]
  end
  
  scope do
    User.includes(:profile)
    .includes(:address)
    .includes(address: :state)
    .includes(address: :city)
    .parceiro
    .order(:name)
  end

  attr_accessor :current_user

  def show_executivos
    return current_user.admin? || current_user.representante?
  end
end

I think you have to add constructor ie initialize method in ruby. Something like following to make it work.

def initialize(params = {})
  @current_user = params.fetch(:current_user)
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM