3

我有一个脚本User.groovyUserController.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)
}

我的用户控制器中的这段代码......

4

3 回答 3

1

addToUserRoles我怀疑在采用参数的显式方法和采用GORM 添加以支持声明的Object参数的隐式方法之间存在某种冲突。尝试为显式方法使用不同的名称以避免干扰 GORM,例如UserRolehasMany

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()
}
于 2013-08-23T10:32:40.667 回答
0

我有一个类似的问题。确保关系在对象的两侧都正确编码,从而修复它。这是描述 1:m、m:m 和 1:1 编码设置的 GORM 文档的链接。

http://grails.org/doc/2.3.x/guide/GORM.html#manyToOneAndOneToOne

于 2014-02-28T18:27:19.323 回答
0

可能您的域与您的数据库不同,请尝试将 Datasource.groovy 更改为再次创建删除以重新创建表。

于 2016-03-10T01:02:04.637 回答