如何在RESTful URL中使用slug?

时间:2012-12-03 13:05:57

标签: seo slug restful-url

根据current best practices,留言板上给定线程的RESTful URL应如下所示:

http://domain/forum/threads/3

URL也应该包含关键字(slug),这也是一种常见的SEO实践,所以上面的URL可能会变成:

http://domain/forum/threads/3/title-of-this-particular-thread

现在,再次根据我在第一段中链接的指南编辑此主题,URL将是:

http://domain/forum/threads/3/edit

当某人创建标题为“编辑”的帖子时会发生什么?如何显示或编辑线程应如何决定?

1 个答案:

答案 0 :(得分:1)

而不是http://domain/forum/threads/3/title-of-this-particular-thread

你应该做http://domain/forum/threads/3-title-of-this-particular-thread

这可以防止冲突,就像SEO友好一样。有几种方法可以实现这一点,但最简单的方法是在模型中添加一个自动执行转换的to_param方法:

class Thread < ActiveRecord::Base
  to_param
    "#{id}-#{title}"
  end
end

如果你需要比这更多的灵活性,或者不想在你的所有模特中重复它,你可以使用friendly_id宝石。

相关问题