我已经在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 = {
}
}
我试图通过此网址保存
所以我做错了什么...请记住堆栈跟踪中没有错误
答案 0 :(得分:2)
我首先将其包装在一个看起来像这样的集成测试中:
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 = "example@example.com"
controller.params.password = "password"
controller.params.signedIn = "false"
controller.register()
def registered = Registered.findByName("SomethingUnique")
assert "example@example.com" == 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
添加到保存调用将导致在未通过验证时抛出异常。如果这个简单的示例工作开始回到更优雅的解决方案,以确定您所处的位置。