Reputation: 187529
In my Grails app I have a couple of domain classes, say Author
and Book
. I'm using the default URL mappings:
static mappings = {
"/$controller/$action?/$id?"{
constraints {
// apply constraints here
}
}
}
So the relative URL to show the book with id 2 is /book/show/2
or for the author with id 5 it's /author/show/5
. Both the Author
and Book
classes have a unique name
property.
I would prefer the URLs to show an author or book use the name rather than the database ID to identify the book/author, e.g. /author/show/shakespeare
and /book/show/ulysses
. Is this achievable without changing the domain classes such that the name field is the id (primary key)?
Upvotes: 1
Views: 466
Reputation: 120286
You could just pass strings (e.g. "shakespeare") as params.id
and then instead of doing a:
Author.get(params.id)
in your controller method, do:
Author.findByName(params.id)
As far as I know, the id
parameter (when used with URLs) doesn't have to be a long
.
Upvotes: 2