2011-08-05 21 views
0

我有两个类用户和用户组编辑和使用表中保存一个特定值的Grails

class User{ 

    String userId 
    String userName 
    UserGroup usergroups 
    static hasMany={usergroups:UserGroup} 
    static belongsTo=UserGroup 
} 

class UserGroup{ 
    String gid 
    static hasMany={users:User} 
} 

现在,我想只需要在用户组单独模板修改值,并保存回了database.In我的用户控制器我写了

def savegroup = { 
    def userInstance = User.get(params.id) 
    if (userInstance.save(flush: true)) { 
     redirect(action: "show") 
    } 
} 

但它显示error.please告诉我什么我需要写在我的控制器?

+1

savegroup不保存组。你得到并保存了一个userInstance,你甚至不修改userInstance。此外,没有告诉我们你得到了什么错误,我们无法帮助你。 – Gregg

+0

错误500:执行控制器[app.UserController]的操作[savegroup]导致异常:无法在空对象上调用方法save() – sree

+0

这是我得到的错误。请告诉我,我需要更改而不是userInstance? – sree

回答

0

您想要修改特定的UserGroup对象。首先你必须确保你传递的params.id是你的用户组的id要改变。那么你必须获取正确的用户组对象而不是用户实例!

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

谢谢tou @ hitty5,我选择了多个值。我使用getAll()方法? – sree

+0

你必须将选定的ID传递给你的控制器。你可以打电话给UserGroup.getAll(params.list ['selectedIds']) – hitty5

+0

谢谢hitty .. – sree

相关问题