使用 Rails 命名路由时如何使参数可选?

发布于 2024-08-31 03:46:24 字数 509 浏览 10 评论 0原文

我有一个命名路由:

map.find '/find/:category/:state/:search_term/:permalink', :search_term=>nil, :controller=>'find', :action=>'show_match'

并且以下 URL 与其匹配 &工作正常:

http://localhost:3000/find/cars/ca/TestSeachTerm/bumpedupphoto-test

但如果我取出第二个最后一个参数,即“TestSearchTerm”,则路线无法匹配,即使我在路线中有 :search_term=>nil 。

http://localhost:3000/find/cars/ca//bumpedupphoto-test

谁能看到我做错了什么吗?这几天正在尝试解决这个问题。

谢谢!

I have a named route:

map.find '/find/:category/:state/:search_term/:permalink', :search_term=>nil, :controller=>'find', :action=>'show_match'

and the following URL matches it & works OK:

http://localhost:3000/find/cars/ca/TestSeachTerm/bumpedupphoto-test

but if I take out the 2nd last parameter i.e. "TestSearchTerm", then the route fails to get matched, even though I have :search_term=>nil in the route.

http://localhost:3000/find/cars/ca//bumpedupphoto-test

Can anyone see what I am doing wrong? Being trying to solve this for a few days now.

Thanks!

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

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

发布评论

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

评论(1

飘过的浮云 2024-09-07 03:46:24

在当前路线之后添加以下内容:

map.find '/find/:category/:state/:permalink', :controller=>'find', :action=>'show_match'

Add this after your current route:

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