【问题标题】:routing to page name路由到页面名称
【发布时间】:2012-01-11 09:55:14
【问题描述】:

有没有办法我可以自动路由 - http://localhost:3000/pages/1http://localhost:3000/home 以及我网站中的所有其他页面,即 - http://localhost:3000/pages/4http://localhost:3000/contact-us

我可以用另一种方式做到这一点 -

match "/home" => 'pages#show', :id => 1
match "/cars-for-sale" => 'pages#show', :id => 1
match "/contact-us" => 'pages#show', :id => 4

但如果可能的话,需要反向并自动执行此操作。

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 routing url-routing custom-routes


    【解决方案1】:

    也许你真正需要的是重定向:

    match "/pages/1", :to => redirect("/home")
    match "/pages/:id", :to => redirect("/contact-us")
    

    请注意,顺序很重要 - “Rails 路线按照指定的顺序匹配”(请参阅​​ http://edgeguides.rubyonrails.org/routing.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-12
      • 2021-12-04
      • 1970-01-01
      • 2021-05-29
      • 1970-01-01
      • 2021-09-12
      • 1970-01-01
      • 2016-03-25
      相关资源
      最近更新 更多