Search code examples
ajaxgrailsgsp

GSP g:submitToRemote call injecting unwanted encoded square brackets around ID


I cannot get my Groovy-Grails GSP to pass an ID in an AJAX g:submitToRemote call without injecting unwanted encoded square brackets around the ID.

I have a top level domain class, Top, that contains a Map of Section classes, called sections, and each Section contains a SubSection.

I need to pass the ID of the SubSection back to the server in the g:submitToRemote call.

I can display the SubSection ID on the GSP with the following, where 'sectionA' is the actual String key for the Map:

${topInsatance.sections.sectionA.subSection.id}

So in my example, the ID is 122.

<g:submitToRemote url="[action:'update', id: topInsatance.sections.sectionA.subSection.id ]" value="Update">

resulting in the bad POST:

.../update%5B122%5D

Just as an experiment, I tried passing just the topInstance.id, which was 116 in my example:

<g:submitToRemote url="[action:'update', id: topInsatance.id ]" value="Update">

resulting in the good POST:

.../update/116

Which has the correct format, but wrong ID. I need the subSection ID.

I also tried:

<g:set var="subSection" value= "${topInsatance.sections.sectionA.subSection}" />

and then:

<g:submitToRemote url="[action:'update', id: subSection.id ]" value="Update">

resulting in the bad POST:

.../update%5B122%5D

I also tried:

<g:set var="subSectionId" value= "${topInsatance.sections.sectionA.subSection.id}" />

and then:

<g:submitToRemote url="[action:'update', id: subSectionId ]" value="Update">

resulting in the bad POST:

.../update%5B122%5D

So it is always putting the encoded brackets around the subSectionId, no matter what I try.

Why does

topInsatance.id 

work, but

subSection.id
subSectionId

not work?

Reference: What does %5B and %5D in POST requests stand for?

Edit: Adding more info about domain classes. The top level domain class is:

class Top {
  Map sections
  static hasMany = [ sections: Section ]
  ...
}

class Section {
  SubSection subSection  // only one
  static belongsTo = [ top: Top ]
  ...
}

class SubSection {
  // some content
}

When the applications runs, a section with the key "sectionA" is added to the Top instance sections Map.


Solution

  • It would seem since you are using a Map you need to make sure Groovy understands you don't expect a collection of values back. So, use the following:

    topInstance.sections['sectionA'].subSection.id