使用Restangular with Rails错误:[$ injector:modulerr]

时间:2014-07-03 22:36:24

标签: ruby-on-rails angularjs restangular

编辑:Restangular取决于Lodash或Underscore !!一旦我安装了Lodash,事情就开始了


如何?我只是想做一个基本的连接来开始...

所以这是我的控制器

AngulaRails.controller "BooksController", ($scope, Restangular) ->

allbooks = Restangular.all('books')

$scope.getBooks = ()  ->
  allbooks.getList().then (response) ->
    console.log(response)
    $scope.books = response

这是我在rails中的视图(我没有弄乱角度的路线,只是为了简单起见):

<div ng-app="AngulaRails" ng-controller="BooksController" ng-init= "getBooks()">
  <h3>booksbooksbooks</h3>
  <div>{{ books }}</div>
</div>

您看到我正在调用getBooks()函数,如控制器中所定义的

这是我的rails控制器:

class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]


  # GET /books
  def index
    @books = Book.all
    @pages = Page.all

    respond_to do |format|
      format.html {}
      format.json {render json: @books, root: false, each_serializer: BookSerializer }

    end
  end

我知道那里有关于root和序列化器等等的一些奇怪的东西,但我也尝试过渲染json:@books无济于事。

我的浏览器会显示Booksbooksbooks,然后只显示{{books}} 以下是来自我的控制台的错误消息:

错误:[$ injector:modulerr] http://errors.angularjs.org/1.2.8/ $ injector / modulerr?p0 = AngulaRails&amp; p1 =%5B%24injector%3Amodulerr%5D%20http%3A%2F%2Ferrors.angularjs.org%2F1。 2.8%2F%24injector%2Fmodulerr%3Fp0%3Drestangular%26P1%3D_%2520is%2520not%2520defined%250Aa.init%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Frestangular.min.js%253Fbody%253D1%253A7% 253A577%250A%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Frestangular.min.js%253Fbody%253D1%253A7%253A10268%250Ad%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js% 253Fbody%253D1%253A31%253A451%250Ag%252F%253C.instantiate%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A32%253A585%250Ac%2540http%253A%252F% 252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A29%253A499%250Aa%252F%253C%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A29% 253A438%250Ae%252F%253C%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangul ar.min.js%253Fbody%253D1%253A30%253A302%250Aq%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A8%253A276%250Ae%2540http%253A%252F% 252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A30%253A123%250Ae%252F%253C%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A30% 253A181%250Aq%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A8%253A276%250Ae%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js% 253Fbody%253D1%253A30%253A123%250A%2524b%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A33%253A238%250AZb%252Fc%2540http%253A%252F%252Flocalhost% 253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A18%253A437%250AZb%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A19%253A141%250ATc%2540http% 253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min的.js%253Fbody%253D1%253A18%253A220%250A%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fangular.min.js%253Fbody%253D1%253A202%253A125%250AjQuery.Callbacks%252Ffire%2540http%253A%252F %252Flocalhost%253A3000%252Fassets%252Fjquery.js%253Fbody%253D1%253A3120%253A1%250AjQuery.Callbacks%252Fself.fireWith%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fjquery.js%253Fbody%253D1%253A3232%253A7 %250A.ready%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fjquery.js%253Fbody%253D1%253A3444%253A3%250Acompleted%2540http%253A%252F%252Flocalhost%253A3000%252Fassets%252Fjquery.js%253Fbody%253D1 %253A3475%253A3%250A%0AF%2F%3C%40http%3A%2F%2Flocalhost%3A3000%2Fassets%2Fangular.min.js%

我觉得Restangular甚至没有被使用......

编辑:Restangular取决于Lodash或Underscore !!一旦我安装了Lodash它正在工作

1 个答案:

答案 0 :(得分:2)

你需要提供lodash,它是restangular的依赖

在application.js

//= require lodash/dist/lodash.min
//= require angular/angular
//= require restangular/dist/restangular