2014-07-26 24 views
0

閱讀Rails的API文檔中,看這個railscast episode後,我試圖用的輔助方法,在我的控制器之一,因爲這樣的:軌道4的ActionController :: Base的helper方法

class PostsController < ApplicationController 
    helper :readers, :otherhelper, :otherhelper2 

    def endpointMethod 
    readers_helper_method() 
    end 

然後在app/helpers/readers_helper.rb

module ReadersHelper 
    def readers_helper_method 
    ... 
    end 
end 

但是,讓我在PostsController調用readers_helper_method一個NoMethodError。 我在做什麼錯?

回答

0
helper :readers 

是這樣的:

require 'readers_helper' 
include ReadersHelper 

你看,他們應該是類方法,而不是實例方法...

所以,你可以調用

class PostsController < ApplicationController 
    helper :readers, :otherhelper, :otherhelper2 

    readers_helper_method() # hint self is current class 

class PostsController < ApplicationController 
    helper :readers, :otherhelper, :otherhelper2 

    def endpointMethod 
    readers_helper_method() # hit self is current action 
    end 
+0

我不認爲我會關注。 移動方法調用不應該改變它是否是類或實例方法? – Bladt