History | Log In     View a printable version of the current page.  
Issue Details (XML | Word | Printable)

Key: GRAILS-3000
Type: Bug Bug
Status: Open Open
Priority: Major Major
Assignee: Graeme Rocher
Reporter: Percy M. Wegmann
Votes: 1
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Grails

Using the default scaffolding template for an Edit view, the "Add" link for a One-to-Many relationship does not cause the correct owner object to be selected in the "Create" editor for the related object

Created: 23/May/08 10:06 PM   Updated: 23/May/08 10:06 PM
Component/s: Scaffolding
Affects Version/s: 1.0.2
Fix Version/s: None

Time Tracking:
Not Specified

File Attachments: 1. File renderEditor.template (8 kb)

Environment: All

Patch Submitted: Yes


 Description  « Hide
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!



 All   Comments   Work Log   Change History      Sort Order: Ascending order - Click to sort in descending order
There are no comments yet on this issue.