在 Rails 3 中创建 SEO 友好的 URL
我目前的 URL 看起来像这样:
things?category_id=6&country_id=17
我希望 URL 看起来像这样:
/printer_cartridges/united_kingdom
Rails 3 中是否有一种方法,无需对路由器中的所有类别和国家/地区进行硬编码以获得我上面想要的 URL ,也许使用 find_by_name
或类似的东西?解决这个问题的最佳方法是什么?
I currently have URLs which look like this:
things?category_id=6&country_id=17
and I would like to have URLs which look like this:
/printer_cartridges/united_kingdom
Is there a way in Rails 3, without hard coding all of the categories and countries in the router to have the URLs as I would like above, perhaps using find_by_name
or the such like? What is the best way to approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
然后,您需要更新操作以使用 params[:category_slug] 和 params[:country_slug] 而不是 ids 查找所有内容。查看 slugged gem 来生成 slugs。
Then you'll need to update your action to look up everything using params[:category_slug] and params[:country_slug] instead of the ids. Look at the slugged gem to generate slugs.
在您的类别模型中添加方法
,其中category_name 和location_name 是您输入存储名称的位置。
In your category model add the method
where category_name and location_name are where you input where you have have the names stored.