0

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
2
  • Can you post the contents of routes.rb? Commented Nov 25, 2013 at 18:57
  • @zeantsoi sure thing! updated Commented Nov 25, 2013 at 19:01

2 Answers 2

1

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?

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

2 Comments

well I don't have an index method. I was just experimenting with Angular, and was trying to show only separate show methods.
@DenisG Did you try changing your Post.query() to a call to get() passing in some ID?
0

The query() method sends a GET request without passing the Post id. That's why your URL is incomplete and you get a 500 error.

Define your factory like it's done in this article and then call

$scope.post = Post.show(id);

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.