Search code examples
ruby-on-railsrubycontroller

How to use controller method from lib with Rails?


I am using Rails 4.2 now.

I created a module under lib directory. I want to call a maintenance method from app/controllers/applicaiont_controller.rb, but it was failed:

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

My lib's module like:

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

Solution

  • It is not a good way to use redirect method in lib module. Move status check logic to controller or helper.

    Thanks to @jphager2.