简体   繁体   English

如何在控制器中设置变量集,在其他类中可用?

[英]How to make a variable set in controller, available in other class?

In my controller I have: 在我的控制器中,我有:

  def index
    @grid = UsersGrid.new(params[:users_grid]) do |scope|
      scope.where(admin: false).page(params[:page]).per_page(30)
    end
    @grid.assets
    if (current_user && current_users.admin?)
      @show_column = true
    else
      @show_column = false
    end
  end

In my grid class: 在我的网格课程中:

class UsersGrid

  include Datagrid
  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

So in the controller I set @show_column , and in the class it should show the column :abc depending on whether @show_column is true or false. 因此,在控制器中,我设置了@show_column ,并在类中根据@show_column是true还是false来显示:abc列。 This goes wrong as @show_column is an instance variable and not available in the grid class. 这是错误的,因为@show_column是实例变量,在网格类中不可用。 So it's nil. 所以它是零。 How should I change this to make this work? 我应该如何更改才能使它正常工作?

You can pass down the variable when creating the UsersGrid instance, then save it in an instance variable there. 您可以在创建UsersGrid实例时传递该变量,然后将其保存在该实例变量中。

In controller: 在控制器中:

def index
  @show_column = (current_user && current_users.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

In the grid class: 在网格类中:

class UsersGrid
  def initialize(*params, show_column)
    super *params
    @show_column = show_column
  end

  # ...
end

You can use attr_accessor for a variable that is not a column of a table. 您可以将attr_accessor用于不是表的列的变量。

def index
    params[:users_grid][:show_column] = (current_user && current_users.admin?)
    @grid = UsersGrid.new(params[:users_grid]) do |scope|
      scope.where(admin: false).page(params[:page]).per_page(30)
    end
    @grid.assets    
  end

class UsersGrid

  include Datagrid
  scope do
    User.order("users.created_at desc")
  end
attr_accessor :show_column

  column(:abc, :header => "abc?", :html => true, :if => proc {self.show_column == true}) do |user|
    image_tag("abc.png", title: "abc") if user.abc
  end
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby gem方法只在控制器中可用? 如果是这样,如何在其他文件中使用它? - Ruby gem method only available in controller? If so, how to make it available in other files? 如何使在Rails引擎的应用程序控制器中定义的方法自动适用于安装它的任何其他引擎? - How to make method defined in a rails engine's application controller automatically available for any other engines which mount it? 如何使模型方法中设置的属性在控制器中可用? attr_accessor不起作用? - How to make attribute set in model method available in controller? attr_accessor not working? 在一个方法中传递时,使变量在控制器的所有方法中可用 - make variable available in all method of controller when passed in one method 如何在rails中全局使用class \\ object? - How to make class\object available globally in rails? 如何在Rails 3.2的视图和控制器中使用下划线方法? - How to make underscore method available in view and controller in rails 3.2? 如何在不创建实例变量的情况下使变量可用 - How to make the variable available without creating instance variable 如何在控制器中将变量设置为文件内容? - How to set a variable in a controller as the contents of a file? 仅在控制器中设置变量时才如何初始化? - How to initialize variable only if it is set up in controller? 如何在Rails中设置控制器范围的变量? - How to set controller-wide variable in Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM