2014-05-11 168 views
0

我試圖訪問幫助器中控制器中定義的helper_method。這是在軌道2.3我工作。在軌道3/4沒有。那是對的嗎?在幫助器中的控制器中定義的訪問helper_method

application_controller.rb:

class ApplicationController < ActionController::Base 
    helper_method :some_method 

end 

application_helper.rb:

module ApplicationHelper 
    def use_some_method 
     some_method # trying to use helper_method defined in controller which worked in rails 2.3? 
    end 
end 

我得到這個錯誤:

Error: undefined local variable or method `some_method' for... 

如果不允許了什麼對 訪問helper_method的方式?

回答

0

定義的方法中application_helper.rb爲: -

module ApplicationHelper 
def use_some_method 
    some_method 
end 
def some_method 

end 
end 

刪除some_method從application_controller.rb

方法還刪除此行從application_controller.rb: -

helper_method :some_method 
+0

我希望在控制器,視圖和幫助器中可訪問的方法 – Srinivas

+0

我已更新該文章 –

+0

幫助器中的方法可以在控制器中訪問嗎? – Srinivas

0

According to the docs,你可以這樣做:

#app/controllers/your_controller.rb 
Class YourController < ApplicationController 
    helper_method :some_method 

    def some_method 
     "hello world" 
    end 
end 

應該然後可以訪問此在你的助手模塊/類。我很感謝你可能試過這個,如果它不工作,這可能是因爲你的控制器沒有加載

我會做的最基本的測試是把它放到application_controller(這總是被稱爲),然後看看你是否可以從你的幫助文件訪問該方法

+0

我已經將它添加到application_controller並嘗試從application_helper訪問它。辛苦工作。 helper_method只能用於我相信的視圖。 – Srinivas

相關問題