在Rails 3中创建SEO友好的URL [英] Creating SEO friendly URLs in Rails 3

查看:100
本文介绍了在Rails 3中创建SEO友好的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有如下网址:

things?category_id=6&country_id=17

,我想使用如下网址:

/printer_cartridges/united_kingdom

Rails 3中有办法吗,而不用硬编码路由器中的所有类别和国家/地区以拥有我上面想要的URL,也许使用 find_by_name 之类的?最好的方法是什么?

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?

推荐答案

match '/:category_slug/:country_slug', :to => 'things#index'

然后,您需要更新操作以使用params查找所有内容[ :category_slug]和params [:country_slug]而不是ID。查看弹sl的宝石以生成弹ug。

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.

这篇关于在Rails 3中创建SEO友好的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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