rails routing and params with a '.' in them

有些话、适合烂在心里 提交于 2019-11-28 07:49:59

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7276125/rails-routing-and-params-with-a-in-them

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!