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’, ‘http://funonrails.com’=&gt; “<a href=”http://funonrails.com>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 #{user.name}" from 'no-reply@example.com' recipients user.email sent_on Time.now @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=> “http://www.example.com/”>&gt; app.change_password_path=> “/change/password”>> app.change_password_url=> “http://www.example.com/change/password”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 😉

Advertisements

About sandipransing

Web Developer #ruby #rails #JS
This entry was posted in Ruby on Rails. Bookmark the permalink.

2 Responses to 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.

  2. This is my first time pay a quick visit at here and i am genuinely pleassant to read all at alone place.

Leave a Reply

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

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s