John
John

Reputation: 137

No signature of method is applicable for argument types: () values: []Possible solutions:(java.lang.Object), (java.lang.Object)

I have a script User.groovy and UserController.groovy. When I run it, I'm getting

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()

My domain class

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

}

My Controller

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])
}

I've added

def userRole = params.userRoles
userRole.each {
    def userrole = Role.get(it)
    println userrole
    def userRoles = userInstance.addToUserRoles(userrole)
}

this code in my user controller...

Upvotes: 3

Views: 25583

Answers (3)

lucasddaniel
lucasddaniel

Reputation: 1789

Probably your Domain are different of your database, try to change the Datasource.groovy to create-drop again to recreate the tables.

Upvotes: 0

Casey
Casey

Reputation: 1

I had a similar problem. Making sure the relationship was correctly coded on both sides of the objects fixed it. Here's a link to the GORM doc describing 1:m, m:m, and 1:1 coding setup.

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

Upvotes: 0

Ian Roberts
Ian Roberts

Reputation: 122414

I suspect there's some sort of clash between your explicit addToUserRoles method taking an Object parameter and the implicit one taking a UserRole parameter that GORM adds to support the hasMany declaration. Try using different names for your explicit methods to avoid interfering with GORM, for example

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()
}

Upvotes: 1

Related Questions