简体   繁体   English

Rails:在控制器的所有动作中可见的属性

[英]Rails :An attribute seen inside all the actions of a controller

How can i create an attribute in a controller class, that can be seen inside all the actions and all the view related to this controller? 我如何在控制器类中创建一个属性,该属性可以在与此控制器相关的所有动作和所有视图中看到? thanks 谢谢

在Controller中创建一个instance variable ,并确保为该变量提供attr_accessor ,以便所有视图都可以访问它。

Create before filter in your controller 在控制器中创建之前过滤器

For Example: 例如:

before_filter :example before_filter:示例

def example def示例

@variable = true @variable =真

end 结束

Now @variable is accessible in every action as well as views 现在@variable在每个动作和视图中都可以访问

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

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