开发者

rails routing and params with a '.' in them

开发者 https://www.devze.com 2023-04-01 04:07 出处:网络
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, che

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'开发者_运维问答

the spec fails with 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"}

but this is fine:

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?


For Rails 3 you could add this to your route:

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

for Rails 2 (AFAIK):

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

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号