如何通过 Rails 使用 lib 中的控制器方法?

How to use controller method from lib with Rails?

我现在正在使用 Rails 4.2。

我在 lib 目录下创建了一个模块。我想从 app/controllers/applicaiont_controller.rb 调用维护方法,但失败了:

NoMethodError in ProductsController#index
undefined method `redirect_to` for #<...>

我的库模块如下:

module One
  class Display
    response = other_method
    if response.status == 200
      data = 'OK'
    else
      redirect_to_maintencance 'Maintenance ...'
    end
    data
  end
end

app/controllers/applicaiont_controller.rb

class ApplicationController < ActionController::Base
  # Other methods

  def redirect_to_maintencance(message = nil)
    redirect_to :maintencance, flash: { maintencance_message: message }
  end
end

在lib模块中使用重定向方法不是一个好方法。 将状态检查逻辑移至控制器或助手。

感谢@jphager2。