【问题标题】:Rails 4 ActionController::base helper methodRails 4 ActionController::base 辅助方法
【发布时间】:2014-07-26 11:20:13
【问题描述】:

阅读 Rails api 文档并观看此 railscast episode 后,我尝试在我的一个控制器中使用辅助方法:

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。 我做错了什么?

【问题讨论】:

    标签: ruby-on-rails-4 helper


    【解决方案1】:
    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
    

    【讨论】:

    • 我不认为我跟随。移动方法调用不应该改变是类还是实例方法?
    猜你喜欢
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-03
    相关资源
    最近更新 更多