Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ending a Rails 2 URL with an IP address causes routing error?

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!

like image 280
dave.io Avatar asked May 07 '10 09:05

dave.io


1 Answers

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!

like image 109
dave.io Avatar answered Nov 17 '22 21:11

dave.io