To reproduce the problem, set up a domain model as follows:
class Container {
static hasMany = {
items: Item
}
}
class Item {
static belongsTo = [ container: Container ]
Container container
}
When using dynamic scaffolding, the "Edit" page for Container will look something like this:
Items:
- Item A
- Item B
...
<<Add Item>>
Clicking the <<Add Item>> link opens the "Create" page for the Item. This page includes a <select> drop-down to choose the owning Container. The select should be defaulted to the Container from which we clicked the <<Add Item>> link, but it is not.
The root cause is that the code to generate the <<Add Item>> link is broken.
Looking in the renderEditor.template from the scaffolding code, you will see the following code on line 74:
pw.println "<g:link controller=\"${property.referencedDomainClass.propertyName}\" params=\"[\"${domainClass.propertyName}.id\":${domainClass.propertyName}?.id]\" action=\"create\">Add ${property.referencedDomainClass.shortName}</g:link>"
There are two problems:
Problem 1 - ${domainClass.propertyName}?.id should not be surrounded by double quotes, but by single quotes. This causes the generated GSP code to never process the params declaration.
Problem 2 - ${domainClass.propertyName} will only work if the back-reference on the Item object happens to have the same name as the type (i.e. container). If the field has a different name (e.g. myContainer), this will fail. The resolution to this is to use ${property.otherSide.name} instead. This will give the actual name of the back-reference on the Item object.
The complete fix for this is to change line 74 of renderEditor.template to the below:
pw.println "<g:link controller=\"${property.referencedDomainClass.propertyName}\" params=\"['${property.otherSide.name}.id':${domainClass.propertyName}?.id]\" action=\"create\">Add ${property.referencedDomainClass.shortName}</g:link>"
I've attached a corrected version of the file.
It's a quick fix - hopefully someone can commit it for me.
Thanks!