>
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
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’=> “<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.
rails c>> app.root_url=> “http://www.example.com/”>> 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-
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 😉
module ApplicationHelper def display_amount(amount) number_to_currency amount, :precision => 0 end end
Helpers on rails consolerails c>> helper.text_field_tag :name, ‘sandip’=> “<input id=”name” name=”name” type=”text” value=”sandip” />”>> helper.link_to ‘my blog’, ‘http://funonrails.com’=> “<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" endend
Accessing helpers in Action Mailersclass 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 consolerails c>> app.root_url=> “http://www.example.com/”>> 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" endend
Optionally you can set value of host parameter from action mailer host configuration usingdefault_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 😉
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.
This is my first time pay a quick visit at here and i am genuinely pleassant to read all at alone place.