Thursday, April 12, 2012

Using helper method in Rials 3 controllers

Here is a rails 3 way to access you helper methods from rails3 controllers.

# 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

Found the solution from the blog of Samnang Chhun.

Reference

No comments: