ng-resource in Rails. AngularJS + Rails tutorial -


i'm trying use ng-resource show index of book titles. ch.11 of angularails, far has been tough.

i know problem has trying to use resource in coffeescript controller, because when $http "get" request specific url, things work fine. here parts of code this:

1.javscripts/application.js

// manifest file that'll compiled application.js, include files // listed below. // // javascript/coffee file within directory, lib/assets/javascripts, vendor/assets/javascripts, // or vendor/assets/javascripts of plugins, if any, can referenced here using relative path. // // it's not advisable add code directly here, if do, it'll appear @ bottom of // compiled file. // // read sprockets readme (https://github.com/sstephenson/sprockets#sprockets-directives) details // supported directives. // //= require jquery //= require jquery_ujs //= require bootstrap.min //= require angular.min //= require angular-resource  //= require angular-application 

2 serializer:

class bookserializer < activemodel::serializer   attributes :id, :title, :author  end 

3 works fine:

angularails.config ["$httpprovider", ($httpprovider) ->   $httpprovider.defaults.headers.common['x-csrf-token'] = $('meta[name=csrf-token]').attr('content')   $httpprovider.defaults.headers.common.accept = "application/json" ] 

4 javascripts/angular-application.js

//= require_self //= require_tree ./angular  angularails = angular.module("angularails", ["ngresource"]); 

5 index controller index i'm trying at:

  # /books   def index     @books = book.all      respond_to |format|       format.html {   }       format.json { render json: @books, root: false, each_serializer: bookserializer }     end   end 

6 factory resource i'm trying use. in case, i'm calling query index:

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 last not least, coffescript controller app:

angularails.controller "bookscontroller", ($scope,  book) ->   $scope.getbooks = () ->     $scope.books = book.query() 

when try run this, console.log give me error saying:

error: book.query not function

there indeed problem factory declaration. not need include allocation of actions request types of 'get', 'post' etc. default setting.

the issue might arise update action shows post request default. however, put request might more reasonable purpose of updating data record. therefore, included {update: {method: "put"}} overwriting default.

credit: angularails book. works:

  angularails.factory "book", ($resource) ->     $resource("/books/:id", {id: "@id"}, {update: {method: "put"}}) 

if, however, want include explicitly, watch out setting brackets correctly.

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"}   }) 

Comments

Popular posts from this blog

google api - Incomplete response from Gmail API threads.list -

Installing Android SQLite Asset Helper -

Qt Creator - Searching files with Locator including folder -