路由到页面名称

时间:2011-12-02 10:48:42

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

有没有办法可以自动路由 - 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

但是如果可能的话,需要反向并自动完成。

1 个答案:

答案 0 :(得分:0)

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

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

请注意,订单很重要 - “Rails路由按照指定的顺序进行匹配”(参见http://edgeguides.rubyonrails.org/routing.html