GET and POST variable hashes in Ruby on Rails «

Comments Off on GET and POST variable hashes in Ruby on Rails

In Rails, you access GET, POST, and routing variables through the params hash. This works almost all the time, except when you duplicate a variable name: routing overwrites GET overwrites POST.

For an app I’m working on I actually had to care where a variable comes from, so I dug for a while to find out how to access the raw hashes. It ain’t pretty, but here it is in case anyone else ends up needing it:

get = CGIMethods.parse_query_paremeters(@request.query_string)
post = CGIMethods.parse_query_parameters(@request.raw_post)
# you'd think you could use @request.query_parameters and
# @request.request_parameters, but they're update()d by route vars
route = @request.path_parameters

(Also, don’t ask about this in #rubyonrails — you’ll just get lectured on how you don’t really want to access the hashes, how you should rename all your variables and URLs, and how it simply isn’t possible. This will be very frustrating and totally unproductive.)