user984621
user984621

Reputation: 48483

Rails + Angularjs: Module 'ngResource' is not available

I am trying to incorporate some Angularjs code into a test application, but cannot make it work. I try to make a call/interaction with the database.

Here's what I did:

Installed the gem 'angularjs-rails' gem.

In application.html.erb:

<html ng-app="MongoAngularApp">
  ...
  <div class="container" ng-controller="PostsCtrl">
    <%= yield %>
  </div>
  ...

Link:

<%= link_to 'Delete', '', :'ng-click' => "destroy(#{post.id})", :'data-confirm' => 'Are you sure?', :id => "post_#{post.id}" %>

The Angularjs code is in the posts.js file:

var app = angular.module('MongoAngularApp', ['ngResource']);

app.factory('Post', function($resource) {
    return $resource("posts/:id", { id: '@id' }, {
        index:   { method: 'GET', isArray: true, responseType: 'json' },
      show:    { method: 'GET', responseType: 'json' },
      update:  { method: 'PUT', responseType: 'json' }
    });
})

app.controller("PostsCtrl", function($scope, Post) {
  $scope.posts = Post.index();

  $scope.deletePost = function(index) {  
        alert("!");
    post = $scope.posts[index];
    Post.delete(post);
    $scope.posts.splice(index, 1);
  }
})

When I run this, in the JS console I see the following error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module MongoAngularApp due to:
Error: [$injector:modulerr] Failed to instantiate module ngResource due to:
Error: [$injector:nomod] Module 'ngResource' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.

I double-checked the module name, and that's good. I tried a simple binding with Angularjs and it was working, so Angularjs should be properly loaded too.

What is wrong with ngResource? I checked some tutorials on the Internet and it was used just as angular.module('angular_app_name', ['ngResource']).

What am I missing here?

Thank you.

EDIT:

I am using AngularJS v1.4.7

Upvotes: 1

Views: 469

Answers (1)

Meeh
Meeh

Reputation: 2646

Make sure you have both of these lines in application.js

//= require angular
//= require angular-resource

Upvotes: 4

Related Questions