简体   繁体   English

自定义Geddy.js型号ID?

[英]Custom Geddy.js Model ID?

I have noticed in Geddy that when I create a model and a subsequent record for that model, I get a very ugly model ID associated with the record. 我在Geddy中注意到,当我为该模型创建模型和后续记录时,我得到一个与记录相关的非常丑陋的模型ID。 Something like: 就像是:

http://localhost:4000/posts/3FEEDE8D-2669-445B-AEA1-A31092A7FEDA

Is there a way to change this? 有没有办法改变这个?

Ideally, I would always want this to be some sort of string. 理想情况下,我总是希望这是某种字符串。 Where it be for a post or user: 它适用于帖子或用户:

http://localhost:4000/posts/this-is-a-post-title
http://localhost:4000/profile/meebix

If this is possible, how should I: 如果可以的话,我应该怎么做:

  1. Configure routes 配置路由
  2. Change primary key for model 更改模型的主键
  3. Other implementation steps I may need 我可能需要的其他实施步骤

Thanks! 谢谢!

Yes, you can change the id if you really want to, but you'll be going off the beaten path there, so it's quite a bad idea. 是的,如果你真的想要,你可以改变你的身份,但你会在那里走出困境,所以这是一个非常糟糕的主意。 Let Geddy handle IDs for you. 让Geddy为您处理ID。

The way I would do this (and certainly how many others have too) is to have a "slugging" function create a slug from the post title, and save that in your database. 我这样做的方式(当然还有其他人也有)是有一个“slu”“功能从帖子标题创建一个slug,并将其保存在你的数据库中。 Then, query on that instead in your show action. 然后,在您的show动作中查询该内容。 You won't have to change your routes. 您不必更改路线。

This is what your query will look like in the show action: 这是您的查询在show动作中的样子:

Post.first({slug: params.id}, function (err, post) {

params.id is whatever string you use in the route /posts/<this string> params.id是您在route /posts/<this string>使用的任何字符串

So once you change your show links to use the slug instead of the ID you will be all set! 因此,一旦你改变你的show链接使用slug而不是ID,你将被设置!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM