Ruby on rails 在Rails中调用资源。AngularJS+;Rails教程
我正在尝试使用ng资源来显示书名索引。这是AngulaRails的第11章,到目前为止,这是非常艰难的 我知道我的问题与尝试使用coffeescript控制器中的资源有关,因为当我使用特定url向我们发送$http“get”请求时,一切正常。以下是我的代码的所有部分: 1.javscripts/application.jsRuby on rails 在Rails中调用资源。AngularJS+;Rails教程,ruby-on-rails,angularjs,coffeescript,ngresource,Ruby On Rails,Angularjs,Coffeescript,Ngresource,我正在尝试使用ng资源来显示书名索引。这是AngulaRails的第11章,到目前为止,这是非常艰难的 我知道我的问题与尝试使用coffeescript控制器中的资源有关,因为当我使用特定url向我们发送$http“get”请求时,一切正常。以下是我的代码的所有部分: 1.javscripts/application.js // This is a manifest file that'll be compiled into application.js, which will include
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require angular.min
//= require angular-resource
//= require angular-application
//= require_self
//= require_tree ./angular
AngulaRails = angular.module("AngulaRails", ["ngResource"]);
2序列化程序:
class BookSerializer < ActiveModel::Serializer
attributes :id, :title, :author
end
4 javascripts/angular-application.js
// This is a manifest file that'll be compiled into application.js, which will include all the files
// listed below.
//
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
//
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
// compiled file.
//
// Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
// about supported directives.
//
//= require jquery
//= require jquery_ujs
//= require bootstrap.min
//= require angular.min
//= require angular-resource
//= require angular-application
//= require_self
//= require_tree ./angular
AngulaRails = angular.module("AngulaRails", ["ngResource"]);
5我试图查看的索引的索引控制器:
# GET /books
def index
@books = Book.all
respond_to do |format|
format.html { }
format.json { render json: @books, root: false, each_serializer: BookSerializer }
end
end
6我试图使用的资源的工厂。在本例中,我调用索引的查询:
AngulaRails.factory "Book", ($resource) ->
$resource("/books/:id")
{
'get': {method: 'GET'},
'save': {method: 'POST'},
'query': {method: 'GET', isArray: true},
'remove': {method: 'DELETE'},
'delete': {method: 'DELETE'}
}
7最后但并非最不重要的是,此应用程序的coffescript控制器:
AngulaRails.controller "BooksController", ($scope, Book) ->
$scope.getBooks = () ->
$scope.books = Book.query()
当我尝试运行此操作时,console.log会给我一个错误提示:
错误:Book.query不是函数工厂声明有问题。它将返回此对象:
{
'get': {method: 'GET'},
'save': {method: 'POST'},
'query': {method: 'GET', isArray: true},
'remove': {method: 'DELETE'},
'delete': {method: 'DELETE'}
}
。。。其query
属性是对象,而不是函数。。。因此出现了错误。这可能就是你的意思
($resource) ->
$resource("/books/:id", {},
{
'get': {method: 'GET'},
'save': {method: 'POST'},
'query': {method: 'GET', isArray: true},
'remove': {method: 'DELETE'},
'delete': {method: 'DELETE'}
})
你的工厂申报单确实有问题。您不需要将操作分配包括到“GET”、“POST”等请求类型,因为这是默认设置 唯一可能出现的问题是默认情况下显示给
POST
请求的更新操作。但是,对于更新数据记录而言,PUT
请求可能更合理。因此,我包括了{update:{method:“PUT”}
,它覆盖了默认值
信贷:安格拉雷的书。
这项工作:
AngulaRails.factory "Book", ($resource) ->
$resource("/books/:id", {id: "@id"}, {update: {method: "PUT"}})
但是,如果要显式地包含它,请注意正确设置括号
AngulaRails.factory "Book", ($resource) ->
$resource("/books/:id", {id: "@id"},
{
get: {method: 'GET'},
save: {method: 'POST'},
query: {method: 'GET', isArray: true},
remove: {method: 'DELETE'},
delete: {method: 'DELETE'}
update: {method: "PUT"}
})
主要的问题是,我有另一家工厂在建,在我的资产管道中紧跟着这家工厂,所以rails使用了它并超越了这家工厂。摇摇头 但是是的,这个散列不是必需的:
{
'get': {method: 'GET'},
'save': {method: 'POST'},
'query': {method: 'GET', isArray: true},
'remove': {method: 'DELETE'},
'delete': {method: 'DELETE'}
}
尝试过了,但仍然得到相同的“Book.query不是函数”错误谢谢你的回答!!主要的问题是,我有另一家工厂在建,在我的资产管道中紧跟着这家工厂,所以rails使用了它并超越了这家工厂****摇摇头****