Rails Rails 里在 helper 中写了方法,怎么在 controller 中调用

wl1991724 · 2015年06月04日 · 最后由 azpokr 回复于 2016年05月03日 · 2788 次阅读

在 rails 的 helper 中我定义了一个 module 在 helper 下,怎么在 controller 中调用呢

Use include, like this:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  include ApplicationHelper

  layout false
end

@flowerwrong 谢谢,我用了 included 一直没发现……

可以在 contoller 中用 view_context 对象来访问所有的 helper 方法

#1 楼 @flowerwrong 您好,我想问下在 controller 中 include Helper 方法之后,直接调用方法名就行了么?不用实例化?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号