Can't use ActionDispatch::Request in Rails middleware because path_parameters get lost

357 views
Skip to first unread message

adamc

unread,
Jan 10, 2011, 3:51:23 PM1/10/11
to Ruby on Rails: Core
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
Reply all
Reply to author
Forward
0 new messages