Grails:保存不起作用

发布于 2025-01-01 13:44:57 字数 1137 浏览 1 评论 0原文

我已经在 grails 中创建了新域,并从控制器中创建了我尝试保存的内容,但数据库中没有保存任何内容。代码如下

控制器

     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)


   }

域名

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


    static constraints = {

    }
}

,我正在尝试通过此网址保存

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

那么我做错了什么...记住堆栈跟踪中没有错误

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

汐鸠 2025-01-08 13:44:57

我首先将其包装在一个集成测试中,如下所示:

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

然后我将首先使控制器操作尽可能简单:

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

这样您可以快速重复测试并找出问题所在。如果未通过验证,将 failOnError:true 添加到 save 调用将导致引发异常。如果这个简单的示例有效,请开始寻找更优雅的解决方案来确定问题所在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文