[英]how to make a variable seen in all views - rails
so i have many controllers and many views. 所以我有很多控制器和很多观点。
i want my variable @random_quote to be evaluated each time every view loads up. 我希望每次加载每个视图时都会评估我的变量@random_quote。
i tried several things in application controller(i thought it should be here?) but none of them worked. 我在应用程序控制器中尝试了几件事(我认为它应该在这里?)但它们都没有用。
so how do i connect these two: @random_quote.body (in view)
and 那么如何连接这两个: @random_quote.body (in view)
和
@random_quote = Quote.find(:random)
(in controller right?) @random_quote = Quote.find(:random)
(在控制器中对吗?)
to bee seen through all my application? 通过我的所有申请看到了什么?
thank you for your answers! 谢谢您的回答!
I doubt you actually need it to be accessible in all views. 我怀疑你确实需要它在所有视图中都可访问。 But you can put @random_quote = Quote.find(:random)
under a method that is called with a before_filter
in your ApplicationController. 但是你可以将@random_quote = Quote.find(:random)
放在一个在ApplicationController中使用before_filter
调用的方法下。 It will then be accessible everywhere. 然后它随处可见。
Like so: 像这样:
before_filter :get_random_quote
def get_random_quote
@random_quote = Quote.find(:random)
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.