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

About this user

Duane Johnson http://blog.inquirylabs.com/

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Firing a controller's action from the console

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.

Render a partial to an instance variable

Normally, if you call "render_partial" within a controller, nothing but the partial will be rendered.

Occasionally, it is useful to render a partial to an instance variable as a string so that the view can still be rendered as normal, and the string can be passed in to the view.

  add_variables_to_assigns
  @content_for_navbar = @template.render_partial 'layouts/public_navbar'
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS