Mongo ids 导致可怕的 URL

发布于 2024-10-13 18:37:58 字数 208 浏览 2 评论 0原文

这听起来可能是一个微不足道的问题,但对于面向消费者的应用程序来说相当重要

将可怕的 mongo id 映射到友好的 id 的最简单方法和最具可扩展性的方法是什么?

xx.com/posts/4d371056183b5e09b20001f9

xx.com/posts/a

M

This might sound like a trivial question, but it is rather important for consumer facing apps

What is the easiest way and most scalable way to map the scary mongo id onto a id that is friendly?

xx.com/posts/4d371056183b5e09b20001f9

TO

xx.com/posts/a

M

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

东北女汉子 2024-10-20 18:37:59

您可以在 mongoid 中创建一个复合键,以使用键宏替换默认 id:

class Person
  include Mongoid::Document
  field :first_name
  field :last_name
  key :first_name, :last_name
end

person = Person.new(:first_name => "Syd", :last_name => "Vicious")
person.id # returns "syd-vicious"

如果您不喜欢这种方式,请检查此 gem: https://github.com/hakanensari/mongoid-slug

You can create a composite key in mongoid to replace the default id using the key macro:

class Person
  include Mongoid::Document
  field :first_name
  field :last_name
  key :first_name, :last_name
end

person = Person.new(:first_name => "Syd", :last_name => "Vicious")
person.id # returns "syd-vicious"

If you don't like this way to do it, check this gem: https://github.com/hakanensari/mongoid-slug

野稚 2024-10-20 18:37:59

在您的集合上定义一个友好的唯一字段(如 slug),对其进行索引,在您的模型上定义 to_param 以返回它:

def to_param
  slug
end

然后在您的查找器中,通过 slug 而不是 ID 进行查找:

@post = Post.where(:slug => params[:id].to_s).first

这将让您为了资源交互的目的,将蛞蝓视为你的有效PK,而且它们更漂亮。

Define a friendly unique field (like a slug) on your collection, index it, on your model, define to_param to return it:

def to_param
  slug
end

Then in your finders, find by slug rather than ID:

@post = Post.where(:slug => params[:id].to_s).first

This will let you treat slugs as your effective PK for the purposes of resource interaction, and they're a lot prettier.

愛放△進行李 2024-10-20 18:37:59

不幸的是,关键的宏已从 mongo 中删除。对于自定义 ID,
用户现在必须覆盖 _id 字段。

class Band
  include Mongoid::Document
  field :_id, type: String, default: ->{ name }
end

Unfortunately, the key macro has been removed from mongo. For custom ids,
users must now override the _id field.

class Band
  include Mongoid::Document
  field :_id, type: String, default: ->{ name }
end
天荒地未老 2024-10-20 18:37:59

这是我用来成功解决这个问题的一个很棒的宝石:Mongoid-Slug

https://github。 com/digitalplaywright/mongoid-slug

它提供了一个很好的界面来跨多个模型添加此功能。如果您想自己动手,至少检查一下他们的实现以获取一些想法。如果您要走这条路,请查看 Stringex gem,https://github.com/rsl/stringex 和acts_as_url 库内。这将帮助您获得漂亮的 url 之间的破折号。

Here's a great gem that I've been using to successfully answer this problem: Mongoid-Slug

https://github.com/digitalplaywright/mongoid-slug.

It provides a nice interface for adding this feature across multiple models. If you'd rather roll your own, at least check out their implementation for some ideas. If you're going this route, look into the Stringex gem, https://github.com/rsl/stringex, and acts_as_url library within. That will help you get the nice dash-between-url slugs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文