Philipp
Philipp

Reputation: 377

Query string manipulation in Rails when identical keys are present

I'm currently working on implementing the Distributed Annotation System-standard for our project openSNP.org, however, I've come across a problem with query strings

The standard prescribes how a user can access multiple areas of a genome (or any other biological data-source) using the query string /features?segment=1:1,999;segment=2:100,1000, where the number before the colon is the chromosome and the other two numbers are start and end-positions on the chromosome.

Unlimited segment=X:a,b strings are allowed so that a user can have a look at different positions and chromosomes using just one query.

The problem I have with Rails is that this query string doesn't work out-of-the-box with the params-array - after all, I always have the same key and the last key always overwrites the previous one, so that in the end, I have only "segment" => "2:100,1000" in my params-array, "segment" => "1:1,999" gets overwritten.

I've thought of using JavaScript to change the query string before it's passed to the controller, but the view used is basic XML and implementing JavaScript seems like overkill in this situation. Is there any way to access the query string in Rails before the params-array is created so that I can simply replace all "segment"s by some counter, or just kick out all "segment"s and keep the coordinates?

Upvotes: 2

Views: 563

Answers (1)

Gareth
Gareth

Reputation: 138110

Rails uses Rack::Utils.parse_nested_query which handles query strings in the way you've seen:

>> Rack::Utils.parse_nested_query "segment=1:1,999;segment=2:100,1000"
=> {"segment"=>"2:100,1000"} 

However, you can get the raw query string from the Rails request object using request.query_string, and then use CGI.parse which handles things more like you're after:

>> CGI.parse "segment=1:1,999;segment=2:100,1000"
=> {"segment"=>["1:1,999", "2:100,1000"]} 

Upvotes: 4

Related Questions