adamc
2011-Jan-10 20:51 UTC
Can''t use ActionDispatch::Request in Rails middleware because path_parameters get lost
Hi, I just encountered a bit of an issue where we call request.params of an ActionDispatch::Request inside a rack middleware right before a Rails 3.0.3 app. The issue is that the path_parameters never appear in the parameters hash if you call request.params before the rails app. It seems that that the requests.params method memoizes itself in "action_dispatch.request.parameters" without the parsed path_parameters. The router seems to properly extract the path params but never calls request.path_parameters= method which would properly reset the memoized ''parameters'' method. You can see the code in question here: # file: action_dispatch/http/parameters.rb # Returns both GET and POST \parameters in a single hash. def parameters @env["action_dispatch.request.parameters"] ||= begin params = request_parameters.merge(query_parameters) params.merge!(path_parameters) encode_params(params).with_indifferent_access end end alias :params :parameters def path_parameters=(parameters) #:nodoc: @symbolized_path_params = nil @env.delete("action_dispatch.request.parameters") @env["action_dispatch.request.path_parameters"] = parameters end You can also see the Router Dispatcher directly grabbing the path_parameters from the environment instead of going to through the request object. #file: action_dispatch/routing/route_set.rb class RouteSet PARAMETERS_KEY = ''action_dispatch.request.path_parameters'' class Dispatcher #:nodoc: def initialize(options={}) @defaults = options[:defaults] @glob_param = options.delete(:glob) @controllers = {} end def call(env) params = env[PARAMETERS_KEY] prepare_params!(params) For now, I''ve moved to using a Rack::Request object which is a shame because I had to manually extract out the subdomain and the bug wasn''t very intuitive. Let me know if I can help with this. Thanks, Adam -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.
Maybe Matching Threads
- Hitting 500 status code on invalid UTF-8 byte sequence in params
- uninitialized constant ActionController::Flash::FlashHash [NameError])
- actionpack-3.0.3/lib/action_dispatch/middleware/rescue.rb
- best_standards_support
- Testing Views with Rails 3 and Rspec2 - Can't stub request.path_params[:controller]