Grails scaffolding link reference problem
I'm using scaffolding for a couple of Controllers for two Domain Classes: 1 Sector to N Items:
class Item {
String name
static belongsTo = [sector:Sector]
....
}
class Sector {
String name
static hasMany = [items:Item]
....
}
When I generated the corresponding scaffolding controllers I used the pattern (class)mgr: Sectormgr.groovy and Itemmgr.groovy.
The problem is that some links are invalid in some of the generated views, for it is assuming the I followed the default开发者_StackOverflow中文版 names for the controllers. For instance:
- if I go to /sectormgr/show/20, the list of items associated with it have the link /item/show/22, instead of /itemmgr/show/22
Is there an easy fix for this? Am I missing something when I create the controllers ?
Thanks in advance
There's a couple ways to address this I believe. The simplest is to stick to Grails' convention of naming your controllers SectorController.groovy, ItemController.groovy, etc.
One other way to handle this that I think will work is to update your grails-app/conf/UrlMappings.groovy. Here is the default scaffolding:
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?"{
constraints {
//apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
You want something like:
class UrlMappings {
static mappings = {
"/${controller}mgr/$action?/$id?"{ //Add mgr after controller
constraints {
//apply constraints here
}
}
"/"(view:"/index")
"500"(view:'/error')
}
}
Changing the URLMappings didn't seem to work for me, and it's a much more global change. Running intall-templates
and changing the links to controllers in src/templates/scaffolding/show.gsp
was the approach I took.
You'll need to restart your server after changing the template.
精彩评论