timpone
timpone

Reputation: 19939

Rails 3 routes handling multiple, non-required parameters

I am porting a zend framework / php app to rails and there is a set of parameters for something like a query.

For example:

/locations/get-all-locations/lat/xxx.xxx/lng/xxx.xxx/radius/5/query/lemonade/updated_prev_hours/72

but could provide variations of this like leaving out the query or distance paramenters (ie basically some could be required in different sets - another question). It would seem like all parameters would need to be named.

Would this be best handled by a static segement like this:

match 'locations/get-all-locations/lat/:lat/lng/:lng/radius/:radius/query/:query/updated_prev_hours/:updated_prev_hours => 'locations#get_all_locations'

Is that all I need and the values will be available in the params hash? Or is there a better strategy for handling complex urls like this?

thx

Upvotes: 2

Views: 763

Answers (1)

Gazler
Gazler

Reputation: 84140

This looks like you are going too deep in the URL and simply using the query string would be more appropriate. However if you are porting and have not other option then you can do this with route globbing.

Something like:

match "locations/get-all-locations/*options" => "locations#get_all_locations"

You can then match these up by doing:

Hash[*params[:options].split("/")]

Or merge them by doing:

params.merge!(Hash[*params[:options].split("/")])

Upvotes: 6

Related Questions