views:

50

answers:

1

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 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!

+4  A: 

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
Ron Gejman
it works this way thank you. i also realised i can also put it into application controller(without before filter). what is actually the difference. is it better to put it into helper? thanks again!
I assumed you wanted to generate the quote once per page view. If you want a new quote every time then you don't need the before_filter.
Ron Gejman