开发者

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 b开发者_JAVA百科etter strategy for handling complex urls like this?

thx


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("/")])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜