我有一个脚本User.groovy
和UserController.groovy
。当我运行它时,我正在
Caught: No signature of method: com.vasco.gs.User.addToUserRoles() is applicable for argument types: (com.vasco.gs.Role) values: [nurse] Possible solutions: addToUserRoles(java.lang.Object), addToUserRoles(java.lang.Object), getUserRoles()
我的域类
package com.vasco.gs
import com.vasco.gs.audit.UserAudit
class User {
static constraints = {
userName blank: false,size:2..25, unique: true
firstName blank:false, size:2..60,matches:"[a-zA-Z1-9_]+"
lastName blank:false, size:2..60,matches:"[a-zA-Z1-9_]+"
middleName nullable:false, blank:false, size:2..60,matches:"[a-zA-Z1-9_]+"
gender blank : true, nullable:true
emailId blank : false
mobileNumber blank : true, nullable:true
password nullable: false,blank: false,size:2..256,password:true
confirmPassword nullable: true, blank: false, size:2..256
activeStatus inList:['Y', 'N']
}
String userName
String password
String confirmPassword
String firstName
String lastName
String middleName
String emailId
String mobileNumber
Gender gender
String activeStatus = 'Y'
static hasMany = [userRoles:UserRole, userLocations: UserLocation]
static transients = [
'confirmPassword',
'activeUsers'
]
static List getActiveUsers(){
return User.findAllByActiveStatus('Y')
}
def beforeInsert() {
password = password.encodeAsSHA()
}
def activate(){
this.activeStatus = 'Y'
}
def inactivate(){
this.activeStatus = 'N'
}
def roles() {
return userRoles.collect { it.role }
}
def locations() {
return userLocations.collect { it.location }
}
List addToUserRoles(role){
UserRole.link this, role
return roles()
}
List removeFromUserRoles(role){
UserRole.unLink this, role
return roles()
}
List addToUserLocations(location){
UserLocation.link this, location
return locations()
}
List removeFromUserLocations(location){
UserLocation.unLink this, location
return locations()
}
String toString() {
"$firstName"
}
}
我的控制器
class UserController {
static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
def UserService
def index() {
redirect(action: "list", params: params)
}
def list(Integer max) {
params.max = Math.max(max ?: User.count(), 1)
[userInstanceList: User.list(params), userInstanceTotal: User.count()]
}
def create() {
[roleList: Role.list(),locationList: Location.list(),userInstance: new User(params)]
}
def save() {
def userInstance = new User(params)
def userRole = params.userRoles
userRole.each {
def userrole = Role.get(it)
println userrole
def userRoles = userInstance.addToUserRoles(userrole)
}
if(UserService.validatePassword(userInstance,params.confirmPassword)){
if (!userInstance.save(flush: true)) {
render(view: "create", model: [userInstance: userInstance, userRoles:userRoles])
return
}
flash.message = message(code: 'default.created.message', args: [
message(code: 'user.label', default: 'User'),
userInstance.id
])
redirect(action: "list")
}
else
render(view: "create", model: [userInstance: userInstance])
}
我添加了
def userRole = params.userRoles
userRole.each {
def userrole = Role.get(it)
println userrole
def userRoles = userInstance.addToUserRoles(userrole)
}
我的用户控制器中的代码......
答案 0 :(得分:1)
我怀疑您使用addToUserRoles
参数的显式Object
方法与采用GORM添加的UserRole
参数的隐式参数之间存在某种冲突,以支持hasMany
宣言。尝试为显式方法使用不同的名称,以避免干扰GORM,例如
List addRole(role){
UserRole.link this, role
return roles()
}
List removeRole(role){
UserRole.unLink this, role
return roles()
}
List addLocation(location){
UserLocation.link this, location
return locations()
}
List removeLocation(location){
UserLocation.unLink this, location
return locations()
}
答案 1 :(得分:0)
我有类似的问题。确保在对象的两侧正确编码关系以修复它。以下是GORM doc的链接,描述了1:m,m:m和1:1编码设置。
http://grails.org/doc/2.3.x/guide/GORM.html#manyToOneAndOneToOne
答案 2 :(得分:0)
您的域可能与您的数据库不同,请尝试将Datasource.groovy更改为create-drop以重新创建表。