0

I mostly followed Ryan Bates' setup for a angular app in rails. In my gemfile:

gem 'angularjs-rails'

and in the application.js:

//= require angular
//= require angular-resource
//= require turbolinks
//= require_tree .

Here is what I believe is all the relevant code from views/pages.home.html:

<body data-ng-app="dithat">
  <div class="container" data-ng-controller="accomplishmentController">
    <p> What'd you do? </p>
    <form ng-submit="submit()">
      <input type="text" ng-model="newAccomp" />
    </form>
    <div data-ng-repeat="accomp in accomplishments | filter:newAccomp" >
      <div class="box" ng-click="addToCount()">
        <div class="accomplishment">
          {{ accomp.name }}
          <a href="#" ng-click="delete()" class="x">x</a>
          <p class="count"> {{ accomp.count }} </p>
        </div>
      </div>
    </div>
  </div>  

  <script type="text/javascript">
    app = angular.module("dithat", ["ngResource"]);
    function accomplishmentController($scope, $resource) { 
      Entry = $resource('/api/users.json');
      console.log(Entry.query());
      $scope.accomplishments = [];
      $scope.submit = function() {
        $scope.accomplishments.unshift({ name: $scope.newAccomp, count: 0 });
        $scope.newAccomp = '';
      }
      $scope.addToCount = function() {
        var currentcount = this.accomp.count;
        this.accomp.count = currentcount + 1;
      }
      $scope.delete = function() {
        index = this.$index;
        $scope.accomplishments.splice(index, 1)
      }
    }

  </script>  
</body>

The code works, as in the app is behaving how it should, however it is not making the resource call. I tried this with $http as well and it didn't work either. What am I missing??!! Thanks a lot!

6
  • Can you elaborate on how you tested that the code is working, and where you are looking and seeing that the resource call is not made? Looking at this code, you are not registering a controller with angular. You have just defined a function that can be used as a controller. Are you missing app.controller('accomplishmentController', accomplishmentController)? Commented Oct 15, 2013 at 19:52
  • Good :). I'll add this as a proper answer then. Commented Oct 15, 2013 at 20:47
  • can i ask one more question even though this is maybe a different question? it's making the call but the call is returning an empty array. localhost:3000/api/users returns a json object that is from the users controller where @users = User.all and i render :json. What is wrong with the query? Commented Oct 15, 2013 at 21:01
  • I think should probably be a separate question, but I think this answer could help. Also if you are expecting an object you should be using Entry.get() rather than Entry.query. See here - query has isArray: true Commented Oct 15, 2013 at 21:06
  • ok thanks for the direction, does this look right to you? Entry = $resource('/api/users'); users = Entry.query(function() { }); console.log(users); Commented Oct 15, 2013 at 21:32

1 Answer 1

1

As per comment:

The accomplishmentController function is defined but it still needs to be registered with angular using

app.controller('accomplishmentController', accomplishmentController)

otherwise it will not be able to be used (and won't necessarily cause any errors).

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.