2011-02-22 25 views
2

嘿傢伙!我遇到了一個讓我瘋狂的問題。查看無法看到在ApplicationController中定義的實例變量,在Rails中

我定義內的實例變量我ApplicationController

def initialize 
    @stylesheets = [] 
end 

當我嘗試從視圖訪問它,說SomeNamespace :: SiteSection(index.html.erb):

<% @stylesheets << "some-stylesheet" %> 
<h1>Blablabla</h1> 

實例變量@stylesheets不可見,即紅寶石表示它沒有被定義。

那麼,我如何讓這個實例變量在視圖中可見?

在此先感謝。

附加信息:

  • 我使用Ruby 1.9.2與3.0.4軌道
  • 我使用的命名空間(該ApplicationController沒有命名空間)

回答

9
class ApplicationController < ActionController::Base 
    protect_from_forgery 
    before_filter :set_var 

    private 
    def set_var 
    @stylesheets = [] 
    end 
end 
+0

非常感謝,男士!它的工作完美無瑕。 – EdMelo 2011-02-22 18:54:30