Ruby on rails 使用AngularJS和Mongoid的CRUD操作部分失败
我正在尝试将AngularJS集成到我的Mongoid powered Rails应用程序中。特别是,我希望基本的CRUD操作能够正常工作 1) 保存一本书有效 2) 编辑失败:错误:Ruby on rails 使用AngularJS和Mongoid的CRUD操作部分失败,ruby-on-rails,angularjs,mongodb,mongoid,Ruby On Rails,Angularjs,Mongodb,Mongoid,我正在尝试将AngularJS集成到我的Mongoid powered Rails应用程序中。特别是,我希望基本的CRUD操作能够正常工作 1) 保存一本书有效 2) 编辑失败:错误:[$resource:badcfg]资源配置错误。预期响应包含对象,但获得数组 3) 删除失败:DELETEhttp://localhost:3000/books 404(未找到) 我怀疑这些错误是特定于Mongoid的,因为我在SQlite和ActiveRecord中尝试了完全相同的代码,没有出现任何问题。Mon
[$resource:badcfg]资源配置错误。预期响应包含对象,但获得数组
3) 删除失败:DELETEhttp://localhost:3000/books 404(未找到)
我怀疑这些错误是特定于Mongoid的,因为我在SQlite和ActiveRecord中尝试了完全相同的代码,没有出现任何问题。Mongoid会出现问题,尤其是应用于单个记录/文档的CRUD操作。有什么不对的建议吗
书籍\u ctrl.js.咖啡
myApp.factory "Book", ($resource) ->
$resource("/books/:id", {id: "@id"}, {update: {method: "PUT"}})
myApp.controller "BooksCtrl", ($scope, Book) ->
$scope.getBooks = () ->
Book.query().$promise.then (books) ->
$scope.books = books
$scope.edit = (book) ->
$scope.book = Book.get({id: book.id})
$scope.delete = (book) ->
book.$delete ->
$scope.getBooks()
$scope.save = () ->
if $scope.book.id?
Book.update($scope.book).$promise.then ->
$scope.getBooks()
else
Book.save($scope.book).$promise.then ->
$scope.getBooks()
$scope.book = {}
books\u controller.rb
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
# GET /books
def index
@books = Book.all
respond_to do |format|
format.html {}
format.json {render json: @books, root: false}
end
end
# GET /books/1
def show
respond_to do |format|
format.html {}
format.json {render json: @book, root: false}
end
end
# GET /books/new
def new
@book = Book.new
end
# GET /books/1/edit
def edit
end
# POST /books
def create
@book = Book.new(book_params)
respond_to do |format|
if @book.save
format.html {render redirect_to @book, notice: 'Book was successfully created.'}
format.json {render json: @book}
else
format.html {render action: 'new'}
format.json {render json: @book.errors, status: :unprocessable_entity}
end
end
end
# PATCH/PUT /books/1
def update
respond_to do |format|
if @book.update(book_params)
format.html {redirect_to @book, notice: 'Book was successfully updated.'}
format.json {render json: @book}
else
format.html {render action: 'edit'}
format.json {render json: @book.errors, status: :unprocessable_entity}
end
end
end
# DELETE /books/1
def destroy
@book.destroy
respond_to do |format|
format.html {redirect_to books_url, notice: 'Book was successfully destroyed.'}
format.json {render json: {message: "Book was deleted."}}
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def book_params
params.require(:book).permit(:title, :author)
end
end
<div ng-app="myApp" ng-controller="BooksCtrl" ng-init="getBooks()">
<div class="container">
<div class="row">
<div class="span12">
<form ng-submit="save()">
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control span12" ng-model="book.author"/>
</div>
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control span12" ng-model="book.title"/>
</div>
<button class="btn btn-success">
Save
</button>
</form>
<hr />
<ul>
<li ng-repeat="book in books">
<div class="btn-group">
<div class="btn btn-mini btn-danger" ng-click="delete(book)">
<i class="icon-trash"></i>
</div>
<div class="btn btn-mini btn-default" ng-click="edit(book)">
<i class="icon-edit"></i>
</div>
</div>
{{$index + 1}} {{book.title}} by {{book.author }}
</li>
</ul>
</div>
</div>
</div>
</div>
class BooksController
books\index.html.erb
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
# GET /books
def index
@books = Book.all
respond_to do |format|
format.html {}
format.json {render json: @books, root: false}
end
end
# GET /books/1
def show
respond_to do |format|
format.html {}
format.json {render json: @book, root: false}
end
end
# GET /books/new
def new
@book = Book.new
end
# GET /books/1/edit
def edit
end
# POST /books
def create
@book = Book.new(book_params)
respond_to do |format|
if @book.save
format.html {render redirect_to @book, notice: 'Book was successfully created.'}
format.json {render json: @book}
else
format.html {render action: 'new'}
format.json {render json: @book.errors, status: :unprocessable_entity}
end
end
end
# PATCH/PUT /books/1
def update
respond_to do |format|
if @book.update(book_params)
format.html {redirect_to @book, notice: 'Book was successfully updated.'}
format.json {render json: @book}
else
format.html {render action: 'edit'}
format.json {render json: @book.errors, status: :unprocessable_entity}
end
end
end
# DELETE /books/1
def destroy
@book.destroy
respond_to do |format|
format.html {redirect_to books_url, notice: 'Book was successfully destroyed.'}
format.json {render json: {message: "Book was deleted."}}
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
@book = Book.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def book_params
params.require(:book).permit(:title, :author)
end
end
<div ng-app="myApp" ng-controller="BooksCtrl" ng-init="getBooks()">
<div class="container">
<div class="row">
<div class="span12">
<form ng-submit="save()">
<div class="form-group">
<label>Author</label>
<input type="text" class="form-control span12" ng-model="book.author"/>
</div>
<div class="form-group">
<label>Title</label>
<input type="text" class="form-control span12" ng-model="book.title"/>
</div>
<button class="btn btn-success">
Save
</button>
</form>
<hr />
<ul>
<li ng-repeat="book in books">
<div class="btn-group">
<div class="btn btn-mini btn-danger" ng-click="delete(book)">
<i class="icon-trash"></i>
</div>
<div class="btn btn-mini btn-default" ng-click="edit(book)">
<i class="icon-edit"></i>
</div>
</div>
{{$index + 1}} {{book.title}} by {{book.author }}
</li>
</ul>
</div>
</div>
</div>
</div>
作者
标题
拯救
-
{{$index+1}{{book.title}}{{{book.author}}
对于您的更新问题,请尝试在book\u ctrl.js.coffee
中对其进行修改:
$resource("/books/:id", {id: "@id"}, {update: {method: "PUT", isArray: true}})
AngularJS需要知道API是否返回数组。API代码中的问题可以通过以下方法解决:
format.json{render json:[@book]}
因为在发生错误的情况下,在我看来,您似乎返回了一个数组。您需要为所有情况返回一致的数据。问题似乎源于Mongoid的序列化行为。我按照说明,按照建议重写了Mongoid的序列化行为
现在解决了我的问题。如果有人能够澄清并提供更全面的解释,说明问题是什么以及为什么必须这样做,我肯定会接受这一正确答案。我正试图删除一本书的实例。看来我已经用另一个帖子的建议解决了这个问题。我正在编辑我的答案,很快就会发布。ThanksI测试了你的答案,结果如下:a)删除作品b)保存作品c)编辑显示错误!只有在刷新页面后,编辑才可见。(错误:Error:[$resource:badcfg]资源配置错误。预期响应包含数组,但得到了对象)您知道我是否需要调整其他内容吗?另一种解决方案适用于a)、b)和c)