路由到页面名称

发布于 2024-12-19 15:54:44 字数 628 浏览 7 评论 0原文

有没有一种方法可以将 - http://localhost:3000/pages/1 路由到 http://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

我可以使用 -但需要反向执行此

is there a way i could route - http://localhost:3000/pages/1 to http://localhost:3000/home and all the other pages in my site i.e. - http://localhost:3000/pages/4 to http://localhost:3000/contact-us automatically?

I can do this the other way around using -

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

but need to do this in revers and automatically if possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橪书 2024-12-26 15:54:44

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

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

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

Perhaps what you really need is a redirect:

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

Note, that the order is significant - "Rails routes are matched in the order they are specified" (see http://edgeguides.rubyonrails.org/routing.html)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文