calvin-brand-light-mode
calvin-brand-dark-mode
∕
⌘K
About Calvin Podcast We Can Do This Book Speaking
aAbout Calvin
Podcast
wWe Can Do This Book
sSpeaking
Back

[Rails] Calling view helpers from your controller

Other
Calvin Correli·Oct 18, 2005· 1 minute
When developing with Rails, I often find myself wanting to use the truncate or pluralize text helpers from a controller when I’m setting the flash notice to something like “Post #{truncate(@post.title)} was successfully saved” or “Updated #{pluralize(@posts.size, “post”)}”.

The methods are instance methods, not module methods, which means they can only be called after they’ve been included in a class. But there’s another way, which is to open up the module and make them also be module methods, like so:

module ActionView
  module Helpers
    module TextHelper
      module_function :pluralize, :truncate
    end
  end
end
Require this in your environment.rb, restart your server, and now you can call them with ActionView::Helpers::TextHelper.truncate and ActionView::Helpers::TextHelper.pluralize.

Btw, I like to have a file lib/actionpack_ext.rb for this and all my other mods to vanilla Rails ActionPack, and just require that file from my environment.rb.

Previous Welcome to the Colbert Report
Next Focus

Comments

Write a comment...
00:00 Recording...
calvin-brand-light-mode
calvin-brand-dark-mode
1 Chestnut Hill Plaza #1139
Newark, DE 19713
United States
Copyright © 2026 Calvin Correli
powered by