0

I'm new to Angular, and I'd really appreciate some help in connecting my Rails backend with my Angular frontend.

In my Angular controller, I want to retrieve JSON data from my Rails backend. Using, jbuilder, I am able to retrieve JSON from this URL: http://localhost:3000/assessments/1.json. The JSON is: {"id":1,"course_id":1,"part_id":1,"lesson_id":1,"type_of":"Quiz"}. This seems okay backend-wise. But, when I try to retrieve that JSON in my Angular controller...

app.controller('ThisController', [
'$scope',
'$resource',
function($scope, $resource){
  var Assess = $resource("/assessments/:id.json", {id: "@id", format: 'json'});
  console.log(Assess);
  console.log(Assess.get());

  Assess.get(function(callbackdata){
      //function is called on success
      console.log(callbackdata);
    }
  );

  $scope.findType = function() {
    $resolved = true;
    $scope.description = 'This is a ' + Assess.type_of + '!';
  };

}]);

...I get this in my console.log...

  1. function Resource(value)
  2. Resource {$promise: Promise, $resolved: false}
  3. Resource {0: "<", 1: "!", 2: "D", 3: "O", 4: "C", 5: "T", 6: "Y", 7: "P", 8: "E", 9: " ", 10: "h", 11: "t", 12: "m", 13: "l",...

The third console.log output is individual string characters broken up from my layout/application.html.erb file!

In my Angular view...

<div>
  <h3>Type:</h3>
    <button ng-click='findType()'>Find Type</button>
  <hr>
  {{description}}
</div>

... I get, This is a undefined! when I call the function for 'findType()'... for {{description}}.

If anyone can tell me where I've gone wrong with $resource, I'd sure appreciate it. PS: I'm set up with the ngResource dependency, so that's not an issue. Thanks so much!!

Matt
  • 811
  • 2
  • 11
  • 20

1 Answers1

0

You can check what angular requests and it's response in the Network tab in Chrome Inspector. Also check Rails' log to see if the format is processed correctly.

PoloniculMov
  • 219
  • 3
  • 9
  • Thanks so much. Should the Rails' log show me the .json url? I can't seem to find that in my log--although I did see my `layout/application.html.erb` file there. My `config/initializers/wrap_parameters.rb` file has... `ActiveSupport.on_load(:action_controller) do; wrap_parameters format [:json] if respond_to?(:wrap_parameters); end`...only. Could that be a factor here. – Matt Jun 11 '15 at 08:52
  • It should say something like `Processing AssignmentsController#show as JSON`. Check the that correct action is called and if the parameters are correct. – PoloniculMov Jun 14 '15 at 05:57