Frage

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!

War es hilfreich?

Lösung

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!

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top