带有'.'的rails路由和参数在他们之中 [英] rails routing and params with a '.' in them

查看:13
本文介绍了带有'.'的rails路由和参数在他们之中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Rails 来保护对仅需要为 Web 应用程序的某些用户提供服务的文件的访问.为此,我有一个控制器方法,它接受有关他们想要访问的文件的信息,检查他们的授权,然后如果他们被授权,则使用 x-sendfile 将其发送给他们.这个概念很好用,除了一个障碍:如果他们请求一个带有 .在其中我的路由不知道如何处理它.在我的路线文件中,我有:

I am using rails to guard access to files that need to be served only to some users of a web app. To do this I have a controller method that accepts information about the file they want to access, checks their authorization, and then if they are authorized uses x-sendfile to send it to them. The concept works fine except for one snag: if they request a resource with a . in it my routing doesn't know to handle it. In my routes file i have:

match 'atb_resources/:guid/:resource'  => 'atb_resources#show', :as => :get_atb_resource, :via => :get

但是如果我在我的规范中尝试这个:

and but then if I try this in my spec:

get 'show', :guid => 'some_guid', :resource => 'blah.txt'

规范失败并显示:

Failure/Error: get 'show', :guid => @atb.guid, :resource => 'blah.txt'
 ActionController::RoutingError:
   No route matches {:guid=>"ABCDEFG5", :resource=>"blah.txt", :controller=>"atb_resources", :action=>"show"}

但这很好:

get 'show', :guid => 'some_guid', :resource => 'blahDOTtxt'

我假设问题出在我的路线上,但我并不真正了解周期如何影响路线.有什么想法吗?

I am assuming the problem is with my routing, but I don't really understand how periods affect routes. Any ideas?

推荐答案

对于 Rails 3,您可以将其添加到您的路线:

For Rails 3 you could add this to your route:

:constraints => { :resource => /.*/ }

对于 Rails 2 (AFAIK):

for Rails 2 (AFAIK):

:requirements => { :resource => /.*/ }

Rails 将尝试将 .txt 解释为没有其中之一的格式说明符.

Rails will try to interpret the .txt as a format specifier without one of those.

这篇关于带有'.'的rails路由和参数在他们之中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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