Quantcast
Channel: Honolulu Hacker
Viewing all articles
Browse latest Browse all 20

Using Rails 3 helpers and routes in the console or a rake task

$
0
0

To use a Rails 3 helper in the console

If this is your helper:

module ItemsHelper
  def custom_item_display(item)
    item.to_s
  end
end

And you have a route that looks like this:

  resources :items

On the console, you would do this:

ruby-1.9.2-p136 :001 > item = "hello world"
ruby-1.9.2-p136 :002 > helper.extend ItemsHelper
ruby-1.9.2-p136 :003 > helper.custom_item_display(item)
 => "hello world" 
ruby-1.9.2-p136 :004 > include ActionController::UrlWriter
 => Object 
ruby-1.9.2-p136 :005 > items_path
 => "/items"

In the rake task you do this:

namespace :example do
  desc "Do example items"
  task :items => :environment do
    item = "Hello World"
    include ItemsHelper
    puts "ItemsHelper.custom_item_display(item) = #{custom_item_display(item)}"
 
    include ActionController::UrlWriter
    puts "items_path = #{items_path}"
  end
end

Hope that helps.


Viewing all articles
Browse latest Browse all 20

Trending Articles