2
votes

i have created new domain in grails and from a controller i've tried to save but nothing get saved in the database.. the code is as follow

controller

     def register={
       String name = params.name
       String email = params.email
       String pass = params.password
       boolean signedIn = params.signedIn
       System.out.println(name + " " + email +" "+ pass+" " + signedIn)
       def rUser = new Registered(params)
      rUser.signedIn = signedIn

      System.out.println(rUser)
       rUser.save(flush:true)


   }

domain

    class Registered {
    String name;
    String email;
    String password;
    boolean signedIn =false;


    static constraints = {

    }
}

and i'm trying to save by this url

http://localhost:8080/egypths/apps/register?name=hegab&[email protected]&password=tom&signedIn=false

so what am i doing wrong ... putting in mind that there's no error in the stack trace

1
What do you get when you perform "println rUser.errors" after the save call? - david

1 Answers

2
votes

I would start by wrapping this in an integration test that would look like this:

import groovy.util.GroovyTestCase
import org.junit.Test

public class RegisterControllerTests extends GroovyTestCase {

    @Test
    void saveAction() {
        def controller = new RegisterController() //or whatever the controller name is
        controller.params.name = "SomethingUnique"
        controller.params.email = "[email protected]"
        controller.params.password = "password"
        controller.params.signedIn = "false"

        controller.register()

        def registered = Registered.findByName("SomethingUnique")
        assert "[email protected]" == registered.email
        assert "password" == registered.password
        assert false == registered.signedIn
    }
}

Then I would start by making your controller action as simple as possible:

def register={
    String name = params.name
    String email = params.email
    String pass = params.password
    boolean signedIn = params.signedIn
    def rUser = new Registered()
    rUser.name = name
    rUser.email = email
    rUser.password = pass
    rUser.signedIn = signedIn

    rUser.save(flush:true, failOnError:true)  //I would remove the failOnError after you identify the issue.
}

This way you can quickly repeat your test and figure out where your problem is. Adding the failOnError:true to the save call will cause an exception to be thrown if it doesn't pass validation. If this simple example works start working back towards a more elegant solution to identify where you're issue resides.