editing and saving a particular values in the table using grails
i have two classes User and UserGroup
class User{
String userId
String userName
UserGroup usergroups
static hasMany={usergroups:UserGroup}
static belongsTo=UserGroup
}
class UserGroup{
String gid
static hasMany={users:User}
}
开发者_StackOverflow中文版Now,i want to take only usergroups in seperate template modify value and save it back to the database.In my User Controller I wrote
def savegroup = {
def userInstance = User.get(params.id)
if (userInstance.save(flush: true)) {
redirect(action: "show")
}
}
but its showing an error.please tell me what do i need to write in my controller??
you want to modify a particular UserGroup object. first of all you have to assure that your passed params.id is the id of your user group to change. then you have to fetch the right user group object and not a user instance!
def savegroup = {
// make sure your're getting the right id!
def userGroup= UserGroup.get(params.id)
// check whether the userGroup is not null
if (userGroup.save(flush: true)) {
redirect(action: "show")
} else {
// log msg
}
}
精彩评论