【问题标题】:How to write custom routes with regex in rails app?如何在 Rails 应用程序中使用正则表达式编写自定义路线?
【发布时间】:2016-06-20 04:51:17
【问题描述】:

在 routes.rb 我们已经有了这样的路由:

get 'articles/*article_name', to: 'article_details#show'

但我们需要另一个路由(如下所示),需要重定向到其他控制器。

get 'articles/sitemap*', to: 'article/sitemap#show'

网址可能在站点地图之后有任何内容(articles/sitemap-abc、articles/sitemap-bcd)。

如何做到这一点?

【问题讨论】:

标签: ruby-on-rails ruby rails-routing ruby-on-rails-4.2


【解决方案1】:

把它放在你的 article_name 路由之前:

get 'articles/:sitemap', to: 'articles/sitemap#show', sitemap: /sitemap-.+/

我假设你的控制器是 articles 并在上面的路径中修复它,如果我错了,你可以将它改回 article

文档在这里:http://guides.rubyonrails.org/routing.html#segment-constraints

【讨论】:

    猜你喜欢
    • 2019-05-12
    • 2012-02-16
    • 2020-05-21
    • 1970-01-01
    • 2016-09-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-07
    • 2012-02-19
    相关资源
    最近更新 更多