Reputation: 1468
I have a few controller actions that require several different HTTP methods, GET and POST for example. Instead of handling this in the controller action code, I decided (incorrectly) that it would be faster and less complex if I put this code into the UrlMappings.groovy class.
Here is what I have so far:
class UrlMappings {
static mappings = {
...
"/$controller/(create|edit)/$id" {
action = [
GET: "editView",
POST: "edit"
]
}
}
}
So, in every controller, if the second URL parameter matches "edit", the user will be forwarded to one of two actions depending on the HTTP method of the request.
Everything works fine until the code reaches my editView
or edit
action where this code:
params.id
evaluates as edit
, instead of as 1
as expected from this example request: /location/edit/1
.
Is this a bug in Grails?
Upvotes: 1
Views: 746
Reputation: 535
I don't think you can use the URL mapping Syntax the way that you are, you would need to split it into two mappings something like:
"/$controller/edit/$id" {
action = [
GET: "editView",
POST: "edit"
]
}
and
"/$controller/create/$id" {
action = [
GET: "editView",
POST: "edit"
]
}
I realise this duplicates the action block, but I don't think there is a way around this other than putting another variable in for your action and deciding what to do based on that.
Sorry if I've misunderstood you - this would have been a comment, but I'm not allowed to post any yet!
Upvotes: 1