I'm just starting out learning Angular and have watched Ryan Bates' Railscast, as well as read BerylliumWork's tutorial, but I'm still struggling to figure out how to access data from my Rails server.
I have a pretty straightforward use case here. I want to access a user's first_name in view.
Here is the view code:
<div ng-controller="UserCtrl">
{{user.first_name}}
</div>
Here is the coffeescript file:
app = angular.module("app", ["ngResource"])
app.factory "User", ["$resource", ($resource) ->
$resource("/users/:id", {id: "@id"}, {
show: {method: "GET"}
})
]
@UserCtrl = ["$scope", "User", ($scope, User) ->
$scope.users = User.query()
$scope.user = User.show(id: **1**)
]
Here is my controller:
def show
respond_with User.find(params[:id])
end
As you can see, I have a hardcoded id in $scope.user and it shows up on the page. Similarly, if I use something like ng-app="user in users"...{user.first_name}, I get the full list of users.
How do I get one instance of user though? Using the instance variable, not my hardcoded "1"?
By default:
In your case, you need $scope.user = User.get({id: 1}).