【问题标题】:Rails: How to redirect all urls from previous model to the new modelRails:如何将所有 url 从以前的模型重定向到新模型
【发布时间】:2013-06-02 05:11:59
【问题描述】:

我是 Ruby on Rails 的新手,我正在开发一个示例应用程序。 我有一个名为Doi 的模型。由于看起来不太直观,我将模型重命名为Destination。

我遇到了一个主要问题:如何将包含旧模型 Doi 的所有 url 重定向到新模型 Destination。

例子:

    0.0.0.0:3000/<action_x>/dois/<action_y> 

应该会自动重定向到

    0.0.0.0:3000/<action_x>/destinations/<action_y>

在我的 routes.rb 中,我使用过:

    match '/**/dois/**' => redirect('/**/destinations/**')

但它没有用。解决这个问题的正确方法是什么?

【问题讨论】:

    标签: ruby-on-rails routing


    【解决方案1】:

    你可以试试

    match '/:before/dois/:after' => redirect('/%{before}/destinations/%{after}')
    

    在Rails Routing Guide中查看更多动态重定向示例


    另一种更可定制的解决方案是:

    match '*before/dois/*after' => redirect {|params| "#{params[:before]}/destinations/#{params[:after]}" }
    

    Rails Routing Guide 中还有更多关于通配符的帮助

    【讨论】:

    • 如何在路由中使用通配符?假设我不一定有“之前”,或者我可能有两个(例如:/something/someother/dois/after)?
    • @harsha_g 我添加了另一个可能更适合的解决方案。
    猜你喜欢
    • 2014-10-05
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-17
    • 1970-01-01
    相关资源
    最近更新 更多