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.
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
]
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'});
]