如何在Rails路线中嵌入多个标签,例如Stack Overflow [英] How to embed multiple tags in Rails routes, like Stack Overflow

查看:92
本文介绍了如何在Rails路线中嵌入多个标签,例如Stack Overflow的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当选择Stack Overflow上的标签时,该标签将添加到URL的末尾。添加第二个标签,并使用 + 分隔符将其添加到URL的第一个标签之后。例如:

When one selects a tag on Stack Overflow, it is added to the end of the URL. Add a second tag and it is added to the end of the URL after the first Tag, with a + delimiter. For example:

http://stackoverflow.com/questions/tagged/ruby-on-rails+best-practices

这是如何实现的?这是路由增强功能还是 TagsController 中包含的某些逻辑?最后,如何提取这些标记以进行过滤(假设它们不在 params [] 数组中)?

How is this implemented? Is this a routing enhancement or some logic contained in the TagsController? Finally, how does one 'extract' these tags for filtering (assuming that they are not in the params[] array)?

推荐答案

Vojto的答案是正确的,但请注意,您还可以在服务器端使用Route Globbing来进行干净处理。定义为 /:controller / * tags 的路由将匹配 / questions / ruby​​ / rails / routing ,并在 questions_controller params [:tags] 将是包含 ['ruby',' rails','routing'] 。请参见路由文档

Vojto's answer is correct, but note that you can also use Route Globbing on the server side to handle this cleanly. A route defined as /:controller/*tags will match /questions/ruby/rails/routing, and in the questions_controller, params[:tags] will be an array containing ['ruby','rails','routing']. See the Routing docs.

这篇关于如何在Rails路线中嵌入多个标签,例如Stack Overflow的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆