Ruby on Rails Console Quick Tips

Dive into your app

Running the app method in rails console gives you an integration session instance, so you can use it just like when you’re a normal integration test for Rails.

>> app.class
=> ActionDispatch::Integration::Session

Example:

>> app.project_path(Project.first)
=> "/projects/130349783-with-attachments"

Example:

>> app.get "/735644780/projects/605816632-bcx.atom" 
=> 200

>> app.response.body
=> "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<feed xml:lang=\"en-US\"  ...

Try out a helper

Getting a console session bootstrapped with Rails’ helpers is also a pain, which helper can fix for you! You could also use it to play with building HTML tags, or any existing Rails helper that ActionView knows about.

>> helper.truncate("Testing", length: 4)
=> "T..." 

>> helper.link_to "Home", app.root_path
=> "<a href=\"/\">Home</a>"

Where does that method come from?

>> Project.instance_method(:trash).source_location
=> ["/Users/qrush/37s/apps/bcx/app/models/project.rb", 90]

Example:

>> app.method(:get).source_location
=> ["/Users/qrush/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/bundler/gems/rails-7d95b814583b/actionpack/lib/action_dispatch/testing/integration.rb", 32]

Original with more details: http://37signals.com/svn/posts/3176-three-quick-rails-console-tips

A+ a-
Clip in Evernote