angularjs和Grails2.5.0保存成功,没有错误,但没有保存到数据库
我一直在寻找类似的情况,如我的,我已经看到,这是常见的错误,但我不能找到正确的解决方案,我的问题。我真的很感激任何帮助 我现在的问题是,当我执行这个命令时,它返回HTTP 200,并在我的终端上打印一行,确认它创建了JSON文件。没有错误,但是当我检查我的数据库时,里面什么都没有 到目前为止,我所尝试的:angularjs和Grails2.5.0保存成功,没有错误,但没有保存到数据库,angularjs,database,grails,h2,angular-resource,Angularjs,Database,Grails,H2,Angular Resource,我一直在寻找类似的情况,如我的,我已经看到,这是常见的错误,但我不能找到正确的解决方案,我的问题。我真的很感激任何帮助 我现在的问题是,当我执行这个命令时,它返回HTTP 200,并在我的终端上打印一行,确认它创建了JSON文件。没有错误,但是当我检查我的数据库时,里面什么都没有 到目前为止,我所尝试的: newBook.save(刷新:true) 从create drop到update编辑我的数据源(但是我的整个数据库都消失了,所以我返回了数据。我只需要在演示过程中保存它,这样我就知道每次应用
class Book {
String name
int page
boolean new
static belongsTo = [student: student]
static constraints = {
}
}
.state("book", {
url: "/book",
templateUrl: "assets/app/partials/book.html",
controller: 'Book',
data: {
requiresLogin: true
}
})
控制器
import grails.converters.JSON
import org.springframework.security.access.annotation.Secured
@Secured('permitAll')
class BookController {
def index() {
render Book.list() as JSON
}
def save() {
def newBook = new Book(request.JSON)
newBook.save(flush: true)
println request.JSON
render(['success': true] as JSON)
}
def show() {
def Book = Book.get(params.id)
render Book as JSON
}
}
前端代码:AngularJS
控制器
应用控制器('Book',函数($scope,store,$state,bookFactory){
服务
app.factory('bookFactory', ['$resource', function ($resource) {
return $resource('api/book',
{
'update': {method: 'PUT'}
})
}])
app.js
class Book {
String name
int page
boolean new
static belongsTo = [student: student]
static constraints = {
}
}
.state("book", {
url: "/book",
templateUrl: "assets/app/partials/book.html",
controller: 'Book',
data: {
requiresLogin: true
}
})
HTML
Book Name:
<input type="text" ng-model="book.name" name="bookName" required="" class="form-control" />
Number of Pages:
<input type="number" ng-model="book.page" name="numberPage" required="" class="form-control" placeholder="Number of Pages"/>
Bought within the last 30 days?
<div class="form-group">
<div class="radio">
<label>
<input type="radio" name="new" value=true ng-model="book.new" >
<font color=green>YES</font>
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="new" value=false ng-model="book.new">
<font color=red>NO</font>
</label>
</div>
</div>
<a ui-sref="book" class="btn btn-primary" name="bookBtn" ng-click="saveBook()"> Next </a>
书名:
页数:
在过去30天内买的?
对
不
下一个
您是否尝试过使用FailOneError:true
保存?当保存无效对象时,它将引发异常。@tylerwal感谢您的建议。我通过这样做阅读并解决了问题。因为**学生和图书类之间存在关系*,并且我没有使用任何约束将其设置为null。Grails他告诉我,我必须为学生类输入一个值,然后才能将其保存到数据库中。