0

I'm trying to pass Param data from view to controller and I'm having trouble. Here's what I'm currently trying to do.

View:

<form action="${doStuffURL}" method='post' params="[keyId: ${mykey.id[i]}]"><g:actionSubmit value="doStuff"/></form>

Controller:

def myObjectService //inject service object

def doStuff = {
     myObjectService.doStuff("${params.keyId}")  //this blows up because it's value of NULL
     myObjectService.doStuff(8)                 //this works fine
}

It gets to the method block because the error log says "no signature of method MyObject.doStuff() is applicable for argument types: values: [null]." Also, I'm able to see ${mykey.id[i]} data from another field, so the data is definitely in the view.

How can I get the controller to read the Param data?

Thanks!

gibsonsg
  • 35
  • 2
  • 11
  • If you view the html source after the page renders, have you verified you get a value back for `myKey.id[i]` ? – Gregg Jan 10 '17 at 17:29
  • Yes, just Inspected it in Chrome and verified it has data - params="[keyId: 8]" – gibsonsg Jan 10 '17 at 18:55
  • you should read what i wrote I have explained CAPITALS many times in my post and comments below try `def myObjectService` ..... myObjectService.doStuff("${params.keyId}") freaking big difference in the declarations a lower case char will make all the difference – V H Jan 10 '17 at 20:31
  • "[keyId: 8]" is a String. This is not the map you are looking for perhaps... – railsdog Jan 11 '17 at 02:51
  • Bah -- nevermind, long day. – railsdog Jan 11 '17 at 03:05

1 Answers1

0

err lots wrong here:

<form action="${doStuffURL}" method='post' params="[keyId: ${mykey.id[i]}]"><g:actionSubmit value="doStuff"/></form>

why not use:

<g:form name="something" controller="yourController" action="youraction">

As you can see above you are having to generate form url (maybe you have your reasons)

Controller:

def doStuff = {
     MyObject.doStuff("${params.keyId}")
}

Differences between action and methods in Grails controllers

So firstly why you should change controller but my actual concern here is MyObject.doStuff

is MyObject.doStuff a static method ?

since that is only when a call like this would work. The link shows a static method. gets called here and it may confuse you due to it calling it via executor.registerSenderFault due to how it is generated working - expandability - for future classes that do same thing. this could have been EmailExecutor.registerSenderFault which is the full class in uppercase like you have declared.

surely it should be a service notice starting with lower case.

myObjectServive.doStuff(stuff)

If above is some real method in MyObject and is not a static method then you need to instantiate the class

MyObject myObject = new MyObject()
myObject.doStuff(stuff)

but in short this is why services exist it is all there to save you all the hassle since they just get injected.

I suggest you do some reading looking around

E2A

def doStuff = {
         println "my params are $params " 
         //do you see keyId in there ?
         //if so what is doStuff expecting as input a string ?:

         // since if you do 
         println "${params.keyId.getClass()}"
         //by default it will be a string you may need to change it from:
         //myObjectService.doStuff("${params.keyId}")
         myObjectService.doStuff(params.keyId as Long)
    }

Personally I don't think it is any of the above edited comments, it still relates to how/what you are injecting. I have seen similar issues. I would suggest you create a brand new service and inject new service as a test and start again - not convinced you were injecting it correctly or if you are the service may be some abstract class rather than a normal service. Or.... you are making some form of similar mistake in the uppercase/lowercase declaration of the service name so you may have created:

MyObjectnameService and calling it using myObjectNameService difference in N in those or.... even worse you have created actual service as myObjectNameService with lowercase name.

test this all again using a much simpler naming convention and create a new service as a test

grails create service MyService

and try again using this service

Community
  • 1
  • 1
V H
  • 8,382
  • 2
  • 28
  • 48
  • MyObject is a service that I inject earlier in the code but didn't show. The service has a method called doStuff(int id) that takes in a value. I was able to get it to work when hard-coding the value in the controller. – gibsonsg Jan 10 '17 at 20:25
  • `MyObject` is not a service it would be def `myObjectService` and would include the word `Service` and start with a lower case `m`. Without the word `Service` even if put in services folder will not be a service dude – V H Jan 10 '17 at 20:29
  • I'm not writing the actual object names I have in code, I'm just making up simple ones. My object starts with lower case and ends with 'Service.' I should have been more exact. Sorry for the confusion. But it is definitely a service. I will make adjustments to my original post. – gibsonsg Jan 10 '17 at 20:34