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

一个人想着一个人 提交于 2019-12-04 08:42:54

问题


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!


回答1:


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!



来源:https://stackoverflow.com/questions/2787527/ending-a-rails-2-url-with-an-ip-address-causes-routing-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!