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

wl1991724 · June 04, 2015 · Last by azpokr replied at May 03, 2016 · 2788 hits

在 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 方法之后,直接调用方法名就行了么?不用实例化?

You need to Sign in before reply, if you don't have an account, please Sign up first.