Skip to content

Instantly share code, notes, and snippets.

@richardkmichael
Created August 3, 2011 13:27
Show Gist options
  • Save richardkmichael/1122623 to your computer and use it in GitHub Desktop.
Save richardkmichael/1122623 to your computer and use it in GitHub Desktop.
Where has ActionController::Dispatcher gone in a Rails 3.1 console?
Loading development environment (Rails 3.0.9)
ruby-1.9.2-p290 :001 > env = Rack::MockRequest.env_for('/')
=> {"rack.version"=>[1, 1], "rack.input"=>#<StringIO:0x00000102bfa628>, "rack.errors"=>#<StringIO:0x00000102bfa6c8>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "PATH_INFO"=>"/", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0"}
ruby-1.9.2-p290 :002 > ActionController::Dispatcher.new.call(env)
=> [200, {"Last-Modified"=>"Wed, 03 Aug 2011 13:08:10 GMT", "Content-Type"=>"text/html", "Content-Length"=>"5780"}, #<Rack::File:0x00000102f7a230 @root="/Users/testuser/Documents/Personal/Source/dispatch-test-rails3-stable/public", @path_info="/index.html", @path="/Users/testuser/Documents/Personal/Source/dispatch-test-rails3-stable/public/index.html">]
Loading development environment (Rails 3.1.0.rc5)
ruby-1.9.2-p290 :001 > env = Rack::MockRequest.env_for('/')
=> {"rack.version"=>[1, 1], "rack.input"=>#<StringIO:0x00000102caa230>, "rack.errors"=>#<StringIO:0x00000102caa2f8>, "rack.multithread"=>true, "rack.multiprocess"=>true, "rack.run_once"=>false, "REQUEST_METHOD"=>"GET", "SERVER_NAME"=>"example.org", "SERVER_PORT"=>"80", "QUERY_STRING"=>"", "PATH_INFO"=>"/", "rack.url_scheme"=>"http", "HTTPS"=>"off", "SCRIPT_NAME"=>"", "CONTENT_LENGTH"=>"0"}
ruby-1.9.2-p290 :002 > ActionController::Dispatcher.new.call(env)
NameError: uninitialized constant ActionController::Dispatcher
from (irb):2
from /Users/testuser/.rvm/gems/ruby-1.9.2-p290@rails31/gems/railties-3.1.0.rc5/lib/rails/commands/console.rb:45:in `start'
from /Users/testuser/.rvm/gems/ruby-1.9.2-p290@rails31/gems/railties-3.1.0.rc5/lib/rails/commands/console.rb:8:in `start'
from /Users/testuser/.rvm/gems/ruby-1.9.2-p290@rails31/gems/railties-3.1.0.rc5/lib/rails/commands.rb:40:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
@richardkmichael
Copy link
Author

Naively trying:

env = Rack::MockRequest.env_for('/')
ActionDispatch::Routing::RouteSet::Dispatcher.new.call(env)

Blows up:

NoMethodError: You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.[]

The route set seems empty:

ActionDispatch::Routing::RouteSet.new.routes

=> []

But a routes do exist, config/routes.rb:

RenderTesting::Application.routes.draw do
  match '/' => 'my#index'

  root :to => 'my#index'
end

Perhaps the rails console does not initialize routes?

In any case, I'd like to render a controller action without the router. I just want MyController.new.index(request), and being able to access #render_to_string would be helpful too.

@richardkmichael
Copy link
Author

Calling a controller action:

env = Rack::MockRequest.env_for('/')
MyController.action(:index).call(env)
r.map { |e| puts e.class }
response = r[2]
response.body

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment