开发者

editing and saving a particular values in the table using grails

开发者 https://www.devze.com 2023-03-26 05:53 出处:网络
i have two classes User and UserGroup class User{ String userId String userName UserGroup usergroups static hasMany={usergroups:UserGroup}

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
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消