以IP地址结尾Rails 2 URL会导致路由错误? [英] Ending a Rails 2 URL with an IP address causes routing error?

查看:65
本文介绍了以IP地址结尾Rails 2 URL会导致路由错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以 http:// servername / find / by / CRITERION / VALUE

CRITERION与VALUE一样,是一组有限的字符串。麻烦的是,在某些情况下VALUE必须是IP地址,这会导致我出现路由错误。

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.

这是我的路线:

  map.find 'find/by/:criterion/:query', :controller => "find", :action => "by"

错误来自Mongrel日志:

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)

如果我访问/ find / by / foo / bar或/ find / by / foo / 1234,我没有问题。我怀疑问题可能是Rails根据URL中的句点推断出的MIME类型,但是我真的不知道如何禁用它。我尝试将:defaults => {:format =>:html}传递给路由,但这会导致Mongrel无法完全启动。

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.

任何帮助表示感谢!

推荐答案

路线通行有效!

我的路线现在为:

map.connect 'find/by/*query', :controller => "find", :action => "by"

这会将/ find / by /之后的所有内容放入数组params [:query] ,每个数组对象一个URL段。对于查询/find/by/ip/1.2.3.4,它看起来像:

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"]

所以我只能引用params [:query] [0]和params [:查询] [1]。

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!

这篇关于以IP地址结尾Rails 2 URL会导致路由错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆