Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
904 views
in Technique[技术] by (71.8m points)

rails routing and params with a '.' in them

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...