对mongo的SEO友好的URL

时间:2011-07-05 06:27:34

标签: ruby-on-rails-3 mongodb mongoid

我想在mongo中将网址posts/:id替换为posts/:title。有没有办法做到这一点。我尝试在模型中使用key

class Post
  include Mongoid::Document
  include Mongoid::Timestamps

  field :title
  key :title
end

但我的网址没有变化,而且我收到此错误Document not found for class Post with id(s)

我错过了吗?

2 个答案:

答案 0 :(得分:3)

你为什么使用

  

字段:键

正确的语法应该是key :field_name 有关如何使用密钥签出的进一步参考 this link and scroll to the bottom

我怀疑它会改变路线。

答案 1 :(得分:2)

如果您使用的是Mongoid而不是ActiveRecord,您可以使用名为slugoid的宝石, slugoid

生成漂亮的网址。

相关问题