用IP地址结束Rails 2 URL会导致路由错误?(Ending a Rails 2 URL with an IP address causes routing error?)

编程入门 行业动态 更新时间:2024-10-24 12:27:31
用IP地址结束Rails 2 URL会导致路由错误?(Ending a Rails 2 URL with an IP address causes routing error?)

我试图以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!

更多推荐

本文发布于:2023-08-01 18:12:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361436.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   错误   结束   地址   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!