0

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.

Community
  • 1
  • 1
J21042
  • 1,214
  • 4
  • 19
  • 33
  • Is `sections` a collection on your domain class? If so then the brackets are there because of that. – Joshua Moore Aug 28 '15 at 15:56
  • Yes, sections is a collection (Map), but I am referencing a specific one, 'sectionA', and getting its subSection ID. (There is only one subSection in each section.) How can I prevent the brackets from getting inserted? – J21042 Aug 28 '15 at 16:01
  • Probably not related to the problem but why do you have quotes around `sectionA` in `topInsatance.sections.'sectionA'.subSection.id`? – Jeff Scott Brown Aug 28 '15 at 16:07
  • Thanks Jeff, you are correct, the quotes around 'sectionA' were not needed, but unfortunately removing them did not solve my bracket problem. – J21042 Aug 28 '15 at 16:17
  • Assuming `sectionA` is a property of an element inside the collection? You might want to post your full Domain classes so we all get a better idea of what you are doing. – Joshua Moore Aug 28 '15 at 17:06
  • "sectionA" is the key value (String) into the Top.sections Map. There are several sections, "sectionA", "sectionB", etc.. The g:submitToRemote should return the ID of the SubSection within a given section, in this case its called "sectionA". I've added more info about the Domain classes. – J21042 Aug 28 '15 at 17:31
  • Out of curiosity what happens when you use `topInstance.sections['sectionA'].subSection.id`? – Joshua Moore Aug 28 '15 at 17:46
  • Thank you Joshua Moore! That was it! .sections.sectionA.subSection.id always caused the brackets to be injected, but .sections['sectionA'].subSection.id worked as expected. – J21042 Aug 28 '15 at 17:51

1 Answers1

1

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

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73