GET and POST variable hashes in Ruby on Rails «
»


Code:
1 comment

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.)


Comments

  1. Watch out for the @request.query_string method, as this doesn’t exist on the TestRequest object provided for functional tests.

    You’ll need to do a little extra work to test this code – re-open the TestRequest class and define the query_string method appropriately.

Leave a Reply

Your email address will not be published.