Last Updated: February 25, 2016
·
2.298K
· mindeavor

Use View Helper Methods in Controller (Rails 3)

http://wowkhmer.com/2011/09/09/use-view-helper-methods-in-rails-3-controller/

Use view_context to get access to all helpers:

# app/controllers/posts_controller.rb
class PostsController < ActionController::Base
  def show
    # ...
    tags = view_context.generate_tags(@post)
    email_link = view_context.mail_to(@user.email)
    # ...
  end 
end

# app/helpers/posts_helper.rb
module PostsHelper
  def generate_tags(post)
    "Generate Tags"
  end
end