开发者

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜