带有“。”的rails路由和参数。在他们中 [英] rails routing and params with a '.' in them
问题描述
我正在使用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'
规范失败,并显示a:
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屋!