Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Firing a controller's action from the console (See related posts)

It seems like a simple task, but here's how you can simulate the calling of a controller's action:

ruby script/console

irb> require 'action_controller/test_process'
irb> require 'application'
irb> require 'site_controller'
irb> request = ActionController::TestRequest.new
irb> response = ActionController::TestResponse.new
irb> request.env['REQUEST_METHOD'] = 'GET'
irb> request.action = "late_employee"
irb> InfoController.process(request,response)


Basically, it's like getting inside of a TestUnit method, but you have to do the dirty work yourself.

Comments on this post

technoweenie posts on Aug 25, 2005 at 19:41
Yikes! That's cool, but a lot of typing :)
bricolage posts on Aug 26, 2005 at 00:32
You can use the load command on a .rb that has all but the last two lines on it!

Thanks Duane, I was wondering how to do that.

Good console post from Robby here:
http://www.robbyonrails.com/articles/2005/08/18/are-you-a-console-master
drbrain posts on Nov 19, 2005 at 00:20
Be careful, requiring action_controller/test_process will cause the test suite to attempt to run on exit. It is better to stuff $" with the correct files to prevent test/unit from firing.
schapht posts on Sep 15, 2007 at 17:16
For something simple like this you can also use (from ./script/console):

app.get '/info/late_employee'

But I haven't been able to get at the response object with that yet, so this was helpful today.

Thanks!

You need to create an account or log in to post comments to this site.


Click here to browse all 4836 code snippets

Related Posts