Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Use Rails Helper Methods In Controller

DZone's Guide to

Use Rails Helper Methods In Controller

·
Free Resource
I wanted to use a helper method in the controller and I found the following trick in http://railscasts.com/episodes/132-helpers-outside-views. In any controller, there is a @template-instance and we can call helper methods like pluralize or strip_tags on it:

class XyController < ApplicationController
  def process_text
    @html = ..
    @ascii = @template.strip_tags(@html)   # call helper method strip_tags
    ..
  end

Ryan is not sure if this use is intended, but it's clear, short and with the current rails version it works fine.

Of cause the explicit call with full path 

ActionController::Base.helpers.some_helper_method

works in the controller like everywhere else. 
Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}