Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails helper modules in controller

I have a controller, I want to create a helper for this controller that I can use without including it. I tried creating a helper with the same name as the controller like so

class Cars::EnginesController < ApplicationController
  def start_engine
    check_fuel
  end
end

and the helper that i created was

module Cars::EnginesHelper
  def check_fuel
    logger.debug("cheking fuel")
  end
end

and the error that i got was

undefined local variable or method `check_fuel' for #<Cars::EnginesController:0x1160e8c80>

is there any conventions that i am missing out?

like image 431
Vignesh Avatar asked Dec 05 '13 12:12

Vignesh


1 Answers

If you really want to use helper methods in controller, you can treat your helper as regular Ruby module (which it is, honestly) and use include:

class Cars::EnginesController < ApplicationController
  include Cars::EnginesHelper
  # ...
end
like image 150
Marek Lipka Avatar answered Oct 02 '22 04:10

Marek Lipka