Accessing View Helpers & Routes in Rails Console, Rake Tasks and Mailers


While looking for accessing rails template tags to be accessed on rails console in order to examine whatever written needs to be correct, found that – rails template tags can be tested on rails console using helper object
module ApplicationHelper def display_amount(amount) number_to_currency amount, :precision => 0 end endHelpers on rails console
rails c>> helper.text_field_tag :name, ‘sandip’=> “<input id=”name” name=”name” type=”text” value=”sandip” />”>> helper.link_to ‘my blog’, ‘’=&gt; “<a href=”>my blog</a>”>> helper.display_amount(2000)=> “$2,000″isn’t cool ? though it’s not finished yet. we can include helper files on console and access custom helper methods too
>> include ActionView::Helpers=> Object>> include ApplicationHelper=> Object>> include ActionView::Helpers::ApplicationHelper^C>> display_amount 2500=> “$2,500″Using same way one can make use helpers in rake tasks file. Make sure environment is getting loaded into rake task.
# lib/tasks/helper_task.rakenamespace :helper do desc 'Access helper methods in rake tasks' task :test => :environment do include ActionView::Helpers include ApplicationHelper puts display_amount 2500 #=> "$2,500" endendAccessing helpers in Action Mailers
class Notifier < ActionMailer::Base add_template_helper(ApplicationHelper) def greet(user) subject "Hello #{}" from '' recipients sent_on @user = user end# app/views/notifier/greet.html.erbHi <%= @user.try(:name)%>,

Greetings !!!You have got <%= display_amount(3000) %>Rails routes on rails console
rails c>> app.root_url=> “”>&gt; app.change_password_path=> “/change/password”>> app.change_password_url=> “”Now you may have got requirement to access routes inside rake tasks. Here is the way to do that-
# lib/tasks/route_test.rakenamespace :routes do desc 'Access helper methods in rake tasks' task :test => :environment do include ActionController::UrlWriter default_url_options[:host] = "myroutes.check" puts root_url #=> "http://myroutes.check" endendOptionally you can set value of host parameter from action mailer host configuration using
default_url_options[:host] = ActionMailer::Base.default_url_options[:hos]
Attention: This is my preferred way to use helpers and routes when needed and there may have other choices to do same. Anyone having better approach please feel free to add your valuable comment. I will definitely incorporate that in further developments.
Got Easy, Cheers 😉

2 thoughts on “Accessing View Helpers & Routes in Rails Console, Rake Tasks and Mailers

  1. Excellent post. I was checking continuously this blog and I am inspired!
    Extremely useful information particularly the ultimate section
    🙂 I maintain such information much. I used to be looking for this particular information for a very long time.
    Thank you and best of luck.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.