GrailsInAction / graina2

Source code for the 2nd edition of Grails in Action
90 stars 92 forks source link

`register2` action does not work #40

Open pledbrook opened 11 years ago

pledbrook commented 11 years ago

It seems that the UserController.register2() action returns a model on two of its code paths. Unfortunately, there is no register2 view, so those code paths result in 404s.

The action should either render the register view or redirect to it. Alternatively, we add a register2 action, but that's my least preferred option.

EdZilla commented 10 years ago

This is not directly related to Peter's view issue above, but is definitely about 'register2' action does not work.

I'm not very familiar with command objects, but it seems that the register form parameters are not being bound through to the command object. The params map in the register2 action is correct, but none of the profile parameters get bound to the urc (UserRegistrationCommand) object in register2 action

It seems that the only way to bind the params map to the UserRegistrationCommand object is by explicit assignment in the register2 action: urc.properties = params is this explicitly necessary? if so, this is missing from the MEAP v13 example code.

Here is the change (with added debug)

def register2(UserRegistrationCommand urc) { log.trace "Executing action: '$actionName' with params '$params' and command object '$urc.properties'"

    urc.properties  = params

    if (urc.hasErrors()) {
        log.debug "urc properties: ${urc.properties}"
        urc.errors.each {
            println it
        }
        return [ user : urc ]
    } else {
        log.debug "urc does not have errors"
        def user = new User(urc.properties)
        user.profile = new Profile(urc.properties)
        if (user.validate() && user.save()) {
            flash.message = "Welcome aboard, ${urc.fullName ?: urc.loginId}"
            redirect uri: '/'
        } else {
            // May not be a unique loginId
            return [ user : urc ]
        }
    }
}
mejiad commented 10 years ago

Hi,

I'm trying to make register2 to work but I get some error messages when I try to add a new user from the browser. The error message that I get is:

/hubbub/user/register2 Class org.hibernate.AssertionFailure Message null id in com.grailsinaction.Profile entry (don't flush the Session after an exception occurs)

mejiad commented 10 years ago

I have trying with this program and the only way that I was able to make work correctly is with the following code:

def register2(UserCommand urc, ProfileCommand prc) {
    log.info "register2: urc: ${urc.loginId}"
    log.info "register2: prc: ${prc.fullName}"
    if (request.method == "POST") {
        if (urc.hasErrors() || prc.hasErrors() ) {
            return [ user : urc, profile : prc ]
        } else {
            def user = new User(urc.properties)
            def profile = new Profile(prc.properties)
            profile.user = user
            user.setProfile(profile)
            if (user.validate() && profile.validate() && user.save()) {
                flash.message = "Welcome aboard, ${prc.fullName ?: urc.loginId}"
                redirect(uri: '/')
            } else {
                // maybe not unique loginId?
                log.info("Maybe not a unique loginId")
                flash.message = "Error al dar de alta el usuario"
                return [ user : urc, profile: prc ]
            }
        }
    }
}

// I created a register2 view con the following modifications:

Register New User

Register New User

/g:hasErrors
${flash.message}
/g:if
User Id
Password
Password Repeat
Full Name
Bio
Email
/g:form

Regards...

pledbrook commented 10 years ago

@mejiad I believe this is because Profile does not belong to User. Try adding static belongsTo = [user: User] to the Profile class. Also, please try the latest code from GitHub. It was working last I tried.