[英]editing and saving a particular values in the table using grails
我有兩個類的用戶和用戶組
class User{
String userId
String userName
UserGroup usergroups
static hasMany={usergroups:UserGroup}
static belongsTo=UserGroup
}
class UserGroup{
String gid
static hasMany={users:User}
}
現在,我只想讓用戶組在單獨的模板中修改值並將其保存回數據庫。在我的用戶控制器中,我寫了
def savegroup = {
def userInstance = User.get(params.id)
if (userInstance.save(flush: true)) {
redirect(action: "show")
}
}
但它顯示錯誤。請告訴我我需要在控制器中寫些什么?
您想要修改特定的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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.