Routing routes and options using. in them

I use rails to protect access to files that should only be served by some users of the web application. For this, I have a controller method that takes information about the file that 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 clue: if they request a resource from. in this my routing does not know how to handle this. In my routes file, I have:

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

but if I try this in my specification:

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

The specification does not work with:

 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 it normal:

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

I assume the problem is with my routing, but I do not understand how periods affect routes. Any ideas?

+6
source share
1 answer

For Rails 3, you can add this to your route:

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

for Rails 2 (AFAIK):

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

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

+8
source

Source: https://habr.com/ru/post/896446/


All Articles