Back Forum Reply New

Data binding

Hello,

This is my situation:

I have a class:Code:

class A {
   static constraints = {   }

Q q
}
Here is the create method in the controller:Code:
   def create = {       def aInstance = new A()       aInstance.properties = params       return [aInstance: aInstance]   }
from the Q controller, I make this calls:Code:

// some code to get the qInstance

redirect(controller: quot;aquot;, action: quot;createquot;, params:[q:qInstance])
When this statement is executed, I have this error in my browser:Code:

Failed to convert property value of type java.lang.String to required type myapp.Q for property q; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [myapp.Q] for property q: no matching editors or conversion strategy found

How should I do include a Groovy object in the redirection, i.e. in the quot;paramsquot; object used in Grails!?Thanks a lot.

parse the id of the qinstance, Code:
// some code to get the qInstance

redirect(controller: quot;aquot;, action: quot;createquot;, params:[id : qInstance.id])
}
then in controller a;Code:
def create = { def qInstance = Q.get(params.id)

}To elaborate on what ajukes has already said, you have to remember that the redirect() call effectively builds a ucl. So,Code:
redirect(controller: quot;aquot;, action: quot;createquot;, params:[id : qInstance.id])
results in a redirect to a ucl of the form

Code:
/myapp/a/create?id=10
That's why your parameters must be strings or have an easily parseable string form (for example numbers).

You see, redirect() gets the *browser* to send a new request, and the browser knows nothing about Groovy or Java objects. This is different to render() or when an action returns a map because the code stays inside the server, so real objects can be passed between an action and a view.

Hope that helps,

Peter
¥
Back Forum Reply New