'Show' action with Angular and Rails-Collection of common programming errors
I’m trying to list the contents of a post using Angular with Rails, but totally stuck.
My controller:
var myApp = angular.module('myApp', ['ngResource']);
myApp.controller('categoryController', ['$scope', 'Post',function($scope, Post) {
$scope.heading = "Hello from angular";
$scope.post = Post.query();
}])
myApp.factory('Post', ['$resource', function($resource) {
return $resource('/posts/:id', { id: "@id" } );
}])
Rails posts controller:
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html
format.json { render json: @post }
end
end
‘Hello from angular’ is showing up fine, however the rest gives me an error:
GET http://localhost:3000/posts/show 500 (Internal Server Error)
Can’t figure out the correct way to route this bad boy. Thank you for any helpful input
Edit: routes.rb
resources :users
resources :hubs, shallow: true do
resources :posts, shallow: true
resources :comments, shallow: true do
collection {post :sort}
end
end
-
The error you are showing is a 500, not a 404. I don’t think this is a routing issue as much as it looks to be that something in your code is raising an unhandled exception.
I’m also a little confused because calling Post.query() should be making a query that routes to your index method, not your show method.
If you are trying to route to a show method you might want to do Post.get({id: someID}), otherwise what does your index method look like?