문제

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. 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.

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. Then, query on that instead in your show action. You won't have to change your routes.

This is what your query will look like in the show action:

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

params.id is whatever string you use in the route /posts/<this string>

So once you change your show links to use the slug instead of the ID you will be all set!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top