Wednesday, October 6, 2010

Use of rails console

Hi all,

We can use our rails console(powerful tool for rails app) for debugging and testing. Mainly console helps us to learn more about Ruby

Normally we are using console to talk with our database, and fetch the objects from db.
ex:
User.first
User.all

But apart from that we can use our console to interact with our application using the object "app"
"app"

app.class # returns this
ActionController::Integration::Session

we can fire(get/post) requests to our application from console itself.

>> app.get "/login"
=> 200
it returns the status code for the handled request

>> app.post "/user_sessions", :user_session => {:email => 'vadivelan@example.com', :password => 'secret'}
=> 302
we can send post request with parameters like this

>> app.response.redirect?
=> true
redirected to some other url

>> app.response.redirect_url
=> "http://localhost/login"
view the redirect url

Hope now you guys can start firing the requests to application from console.

Thanks,
Vadivelan