我在试图访问数据网格(gem(中的current_user attr_accessor时遇到了问题,我通过控制器传递current_user(我将在下面发布代码(,然后我应该在数据网格中访问它,如此之多,以至于在其中一个方法(show_executives(中我可以访问它来生成条件,然而,在类的其他部分,主要是在过滤器中,我无法访问它,谢谢!
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
我认为您必须添加构造函数,即在ruby中初始化方法。像跟随这样的东西让它发挥作用。
def initialize(params = {})
@current_user = params.fetch(:current_user)
end