I have domain classes as:
package mnm.schedule
class Project {
static hasMany = [ tasks : Tasks , users : User ]
String name
static constraints = {
name(nullable:false)
tasks(nullable:true)
}
}
User.groovy
package mnm.schedule
import org.example.*;
class User extends SecUser {
//relationships. . . .
static belongsTo = [ company : Company, role : Role, resource : Resource]
static hasMany = [ holidays : Holiday, tasks : Tasks, pt:String ]
Profile profile
Project project
String username
String password
boolean enabled
List pt
boolean ptTaken
}
I have a view file, inside which i have this code snippet :
<g:each in="${ans}">
<li>${it.username.toUpperCase()}<g:checkBox name="checkedUsers" value="${ans}" checked="false" /></li>
</g:each>
The variable ans is the arraylist, that has user objects. I use g:checkBox, so that end user can “check” the required users name.When the user submits this form, I do this action in my controller:
def users = params.checkedUsers
users.each { index ->
new Project(name:"testing",users:index).save()
}
The idea is that I need to add the choose user(via checkbox) to the project.
But this throws the error as :
2012-02-03 10:13:08,173 ["http-bio-8080"-exec-4] ERROR errors.GrailsExceptionResolver - TypeMismatchException occurred when processing request: [POST] /scheduleNew/project/project - parameters:
_checkedUsers:
_checkedUsers:
Add: Add
checkedUsers: anto2
Provided id of the wrong type for class mnm.schedule.User. Expected: class java.lang.Long, got class java.lang.String. Stacktrace follows:
Message: Provided id of the wrong type for class mnm.schedule.User. Expected: class java.lang.Long, got class java.lang.String
Whats going on? Where I went wrong?
Seems a few things a bit odd to me (but maybe I am not fully understanding what you are trying to achieve).
View:
I would pass the id rather than the entire list (you had value=”${ans})
In the controller, the passed params are of type String, that’s why you are getting the type mismatch. There are several ways to do this, one solution is below:
Controller:
I’m sure you can simplify this further.