0

I’m trying to use Angularjs to get JSON from a Rails test app that I put on Heroku. Below you will find my Angular and Rails code.

This is the error that I get in my Firebug console. "NetworkError: 404 Not Found - http://desolate-earth-2852.herokuapp.com/declarations.json"

Is there a reason this is not working?

Angularjs code

var App = angular.module('App', []);

App.controller('TodoCtrl', function($scope, $http) {
  $http.get('http://desolate-earth-2852.herokuapp.com/declarations.json')
       .then(function(res){
          $scope.todos = res.data;        
        });
});

Rails code

  def index
    @declarations = Declaration.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @declarations }
    end
  end

Here are the routes

Test::Application.routes.draw do
  root :to => 'welcome#index'
  get "welcome/index"
  resources :declarations
end
5
  • Do You have routes defined like this: resources :declarations ? Commented Oct 16, 2013 at 21:31
  • yes, post your routes.rb code Commented Oct 16, 2013 at 22:01
  • I have added the routes. I had resources :declarations already. Commented Oct 17, 2013 at 5:10
  • that's odd you get a 404. Are you running the angular code also from the same herokuapp? Could you specify the url where the angular code runs? Commented Oct 17, 2013 at 11:42
  • @ Manuel van Rijn. I'm running the Angular code locally on my desktop Commented Oct 17, 2013 at 13:13

1 Answer 1

5

It might be because of Rails JS minification. Ryan Bates talks about this in his Railscast about integrating AngularJS. I notice that you have nothing mentioning that you were accounting for the minification in your config files, and if you aren't, then that means that all of your Dependency Injection in your Angular code needs to be put into and array, like this:

App.controller('TodoCtrl', ['$scope', '$http', function($scope, $http) {
  $http.get('http://desolate-earth-2852.herokuapp.com/declarations.json')
    .then(function(res){
       $scope.todos = res.data;        
    });
}]);

Instead of this:

App.controller('TodoCtrl', function($scope, $http) {
  $http.get('http://desolate-earth-2852.herokuapp.com/declarations.json')
   .then(function(res){
      $scope.todos = res.data;        
   });
});

This tells AngularJS what the dependencies are so that if the names do change (during minification) it won’t matter. You have to do this for every function that accepts services, for example the one you have here for creating the app's controller.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.