Rails 3 - variable available in all controllers
I need to have a variable, that will be available in all controllers. For example, I tried something like:
class ApplicationController < ActionController::Base
@test = 'test string'
...
end
and if I tried to show the content of @test in application.html.erb
, so I got the empty result.
One possibility is store th开发者_JAVA技巧e string to session
, but this way I would like to use only as the latest possibility...
So I would like to ask you - exist in Rails any elegant way, how to do?
You could use a before_filter
to set the variable:
class ApplicationController < ActionController::Base
before_filter :set_test
def set_test
@test = 'test string'
end
end
With this you can use @test
in application.html.erb.
精彩评论