ruby on rails-route.rb-当文件名中存在多个句点时匹配文件扩展名 [英] ruby on rails - routes.rb - match file extension when multiple periods exist in filename

查看:115
本文介绍了ruby on rails-route.rb-当文件名中存在多个句点时匹配文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照此处的说明创建了一个路线加控制器,用于在Rails上的动态CSS中进行动态CSS:

I have created a route plus controller for doing dynamic css in ruby on rails as per the instructions here:

http://www.misuse.org/science/2006/09/26/dynamic -css-in-ruby-on-rails /

需要一些更改以考虑到较新版本的Rails红宝石,但问题来了与routes.rb条目。原始条目是这样的:

It took some changing to account for a newer version of ruby on rails, but the problem comes in with the routes.rb entry. The original entry was this:

  # dynamic CSS (stylesheets)
  map.connect 'rcss/:rcssfile',
    :controller => 'rcss',
    :action => 'rcss'

这不适用于较新版本的RoR,我发现此解决方案可以工作:

This did not work with a newer version of RoR, and I found this solution to work:

  # dynamic CSS (stylesheets)
  map.connect 'rcss/:rcssfile.css',
    :controller => 'rcss',
    :action => 'rcss'

但是,现在我很沮丧,无法获得所有文件类型的扩展名处理程序。该请求必须具有.css扩展名。

However, now I was bummed that I couldn't get a catch-all filetype extension handler. The request had to have the .css extension. Playing around with it further I came up with this:

  # dynamic CSS (stylesheets)
  map.connect 'rcss/:rcssfile.:format',
    :controller => 'rcss',
    :action => 'rcss'

所以这要好得多。现在,我可以潜在地请求一个以.foobar或其他结尾的文件,并将其与处理程序匹配。

So this is much better. Now I could potentially request a file that ended in .foobar or whatever and match it with a handler. Not that I would necessarily, but it's more about understanding everything.

所以我随后尝试创建一个看起来像 foo.net.rcss的文件。现在,似乎第一个点将所有内容弄乱了。 没有路由匹配rcss / foo.net.css。我的问题如下:

So then I tried creating a file that looked something like "foo.net.rcss" . Now it would seem that the first dot messes everything up. "no routes match rcss/foo.net.css". My questions are as follows:


  1. 无论文件名中有多少点,如何匹配任何文件名和任何扩展名?

  1. How can I match any filename and any extension regardless of how many dots are in the filename?

为什么第一个示例在更高的RoR版本中不起作用?

Why does the first example not work in later RoR versions?

为什么

预先感谢您的帮助。

-------更新-------

------- update -------

我正在使用Rails 3.0.5。根据更多研究,我可以将语法缩短为:

I am using Rails 3.0.5 . As per some more research I can shorten the syntax to:

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

这等效于第一个似乎无效的示例

This is the equivalent of the first example that did not seem to work, however using this syntax it works just as expected.

match 'rcss/:rcssfile:.:format', :to => 'rcss#rcss'

这也与我之前的示例#3一样,但是它仍然具有

This also works just like my previous example #3, however it still has the problem of not matching a file with multiple periods.

推荐答案

标记标准:paramater似乎需要特别注意期间字符。 :parameter将匹配最多一个句点的路径,:parameter.:extension将匹配最多两个句点的路径,但是:extension仅是两个句点之间的值,等等。

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.

一种解决方法是使用所谓的 Route Globbing,它使用星号而不是冒号:

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.

这篇关于ruby on rails-route.rb-当文件名中存在多个句点时匹配文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆