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!
Any instance variable created in a controller is available in the view file.
Rails Guides describes partials this way: Partial templates - usually just called "partials" - are another device for breaking the rendering process into more manageable chunks. With a partial, you can move the code for rendering a particular piece of a response to its own file.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With