我试图以http:// servername / find / by / CRITERION / VALUE的格式构造URL
CRITERION是一组有限的字符串,VALUE也是如此。 麻烦的是,VALUE在某些情况下需要成为IP地址,并且会导致路由错误。
这是我的路线:
map.find 'find/by/:criterion/:query', :controller => "find", :action => "by"从Mongrel日志中的错误:
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)如果我访问/ find / by / foo / bar或/ find / by / foo / 1234我没有问题。 我怀疑问题可能是Rails根据URL中的句点推断MIME类型,但我真的不知道如何禁用它。 我试过给路由传递一个:defaults => {:format =>:html},但是导致Mongrel无法完全启动。
任何帮助感谢!
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!
最满意答案
路线通配工作!
我的路线是:
map.connect 'find/by/*query', :controller => "find", :action => "by"这将每个数组对象之后的所有内容都放在Array,params [:query]之后。 对于查询/find/by/ip/1.2.3.4,这看起来像:
["ip", "1.2.3.4"]所以我可以参考params [:query] [0]和params [:query] [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!
更多推荐
发布评论