Helper Methods in the Rails Console

Written by Bennett on February 02, 2017

Going through the Rails Tutorial, I kept getting an error when I was writing a test for a helper method. I knew what the method returned, but my tests were still failing.

Rails Console to the Rescue!

Here’s my helper module:


module ApplicationHelper
  # Returns the full title on a per-page basis
  def fulltitle(pagetitle = ' ')
    basetitle = "Ruby on Rails Tutorial Sample App"
    if pagetitle.empty?
      basetitle
    else
      pagetitle + " | " + base_title
    end
  end
end

Getting the helper in the rails console is as simple as including it. Start the console with the rails c command.

$ rails c

irb(main):001:0> include ApplicationHelper
=> Object
irb(main):002:0> helper.full_title("Contact")
=> "Contact | Ruby on Rails Tutorial Sample App"


It’s that simple.