ruby on rails - routes.rb - match file extension when multiple periods exist in filename

这一生的挚爱 提交于 2019-12-06 22:26:26

It would seem that labeling a standard ":paramater" takes special consideration for the period character. ":parameter" will match a path with up to one period, ":parameter.:extension" will match a path with up to two periods, but the :extension will be only what's between the two periods, etc.

A way around this is to use what is called "Route Globbing", which uses an asterisk instead of a colon:

match 'rcss/*rcssfile', :to => 'rcss#rcss'

The only caveat is that this will match ANYTHING after the asterisk, including subdirectories. As such, you want to make sure that this does not accidentally expose any secure files or accidentally render things unintentionally.

Use a regex to match the filename?

map.connect 'rcss/:rcssfile',
  :controller => 'rcss',
  :action => 'rcss',
  :requirements => {:rcssfile => /.+\.rcss/ }

This would match (anything).rcss - you could adjust the regex for various suffixes.

I used this for a general case when you don't know the extension:

get '/uploads/:basename.:extenstion', to: 'controller#action', basename: /.*(?=\.[\w\d]+$)/
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!