I'm trying to construct URLs in the format http://servername/find/by/CRITERION/VALUE
CRITERION is a finite set of strings, as is VALUE. Trouble is, 开发者_运维问答VALUE needs to be an IP address in some situations, and it's causing me a routing error.
Here's my route:
map.find 'find/by/:criterion/:query', :controller => "find", :action => "by"
And the error, from the Mongrel logs:
Processing ApplicationController#index (for 127.0.0.1 at 2010-05-07 10:20:32) [GET]
ActionController::RoutingError (No route matches "/find/by/ip/1.2.3.4" with {:method=>:get}):
Rendering rescues/layout (not_found)
If I visit /find/by/foo/bar or /find/by/foo/1234 I don't have problems. I suspect the problem might be Rails' inference of MIME types based on periods in the URL, but I don't really know how I can disable that. I've tried passing a :defaults => {:format => :html} to the route but that causes Mongrel to fail to start entirely.
Any help appreciated!
Route globbing worked!
My route is now:
map.connect 'find/by/*query', :controller => "find", :action => "by"
This puts everything following /find/by/ into an Array, params[:query], one URL segment per array object. For the query /find/by/ip/1.2.3.4, this looks like:
["ip", "1.2.3.4"]
So I can just refer to params[:query][0] and params[:query][1].
If anyone has a better way of doing it, please post it!
精彩评论