2014-07-26 26 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