I have a controller method that passes an instance variable @show_column to a grid class. The problem is that although the value of the instance variable is available in def initialize of the grid class, it is nil outside def initialize:
The controller method:
def index
@show_column = (current_user && current_user.admin?)
@grid = UsersGrid.new(params[:users_grid], @show_column) do |scope|
scope.where(admin: false).page(params[:page]).per_page(30)
end
@grid.assets
end
The grid class:
class UsersGrid
include Datagrid
attr_reader :show_column
def initialize(*params, show_column)
super *params
@show_column = show_column
end
scope do
User.order("users.created_at desc")
end
column(:abc, :header => "abc?", :html => true, :if => proc {@show_column == true}) do |user|
image_tag("abc.png", title: "abc") if user.abc
end
end
The problem is at the end with: proc {@show_column == true}. This doesn't work because @show_column is always nil. I've used a debugger to try to find some additional information. I shows that @show_column is set correctly in the controller; its value is true as it should be given the user I logged in with. Also, inside def initialize its value is true. However, outside def initialize in the grid class, its value is nil. Any ideas how to solve this?
:if => proc {@show_column == true})line in yourUsersGridclass to this::if => proc {show_column == true})and then tryundefined local variable or method 'show_column' for UsersGrid:Class.@show_columnis expected to be a class variable not instance variable. Could you explain from where you're writing such a code?UsersGridclass looks like a Ruby class with no active record... is it coming fromDatagridmodule?