开发者

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.

i tried several things in application controller(i thought it should be here?) but none of them worked.

so how do i connect these 开发者_C百科two: @random_quote.body (in view) and

@random_quote = Quote.find(:random) (in controller right?)

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. It will then be accessible everywhere.

Like so:

before_filter :get_random_quote
def get_random_quote
    @random_quote = Quote.find(:random)
end
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜