0

I'm using rails 3.2 with angularjs, and I'm trying to understand how to use angular's $resource.

I would like resource to return the results from a non-default action (so not get, save, query, remove, and delete)

So in my Posts controller I have

def home
  @recent_posts = Post.recent_posts
  respond_to do |format|
    format.html
    format.json
  end
end

And then in my js file I have

app.factory "Post", ["$resource", ($resource) ->
  $resource("/post/:id/:action", {id: "@id"}, {home: {method: "GET", isArray: true}})
]

@HomeCtrl = ["$scope", "Post", ($scope, Post) ->
  $scope.recent_posts = Post.home()
]

In my view if I try and do something like ng-repeat="post in recent_posts", it's not returning anything.

1
  • $scope.recent_posts = Post.home({action:'home', function(response){console.log(response) }, function(response){console.log(response)} }). Please have these callbacks registered and check whats coming in the response. The first function is the success callback, the second one is the error callback. Commented Mar 21, 2013 at 8:00

2 Answers 2

1

You forgot to specify the action parameter:

app.factory "Post", ["$resource", ($resource) ->
  $resource "/post/:id/:action",
    id: "@id"
  ,
    home:
      method: "GET"
      params:
        action: "home"

      isArray: true

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

Comments

0

Thanks for your suggestions, they helped me get to what eventually worked. I just changed the Home Controller like so:

@HomeCtrl = ["$scope", "Post", ($scope, Post) ->
  $scope.recent_posts = Post.home({action:'home'});
]

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.