{"id":1797,"date":"2022-08-30T15:19:31","date_gmt":"2022-08-30T15:19:31","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/show-action-with-angular-and-rails-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:31","modified_gmt":"2022-08-30T15:19:31","slug":"show-action-with-angular-and-rails-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/show-action-with-angular-and-rails-collection-of-common-programming-errors\/","title":{"rendered":"&#39;Show&#39; action with Angular and Rails-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;m trying to list the contents of a post using Angular with Rails, but totally stuck.<\/p>\n<p>My controller:<\/p>\n<pre><code>var myApp = angular.module('myApp', ['ngResource']);\n\nmyApp.controller('categoryController', ['$scope', 'Post',function($scope, Post) {\n    $scope.heading = \"Hello from angular\";\n    $scope.post = Post.query(); \n}])\n\nmyApp.factory('Post', ['$resource', function($resource) {\n    return $resource('\/posts\/:id', { id: \"@id\" } );\n}])\n<\/code><\/pre>\n<p>Rails posts controller:<\/p>\n<pre><code> def show\n    @post = Post.find(params[:id])\n\n    respond_to do |format|\n      format.html\n      format.json { render json: @post }\n    end\n  end\n<\/code><\/pre>\n<p>&#8216;Hello from angular&#8217; is showing up fine, however the rest gives me an error:<\/p>\n<pre><code>GET http:\/\/localhost:3000\/posts\/show 500 (Internal Server Error) \n<\/code><\/pre>\n<p>Can&#8217;t figure out the correct way to route this bad boy. Thank you for any helpful input<\/p>\n<p>Edit: routes.rb<\/p>\n<pre><code>  resources :users\n\n  resources :hubs, shallow: true do\n    resources :posts, shallow: true\n    resources :comments, shallow: true do\n      collection {post :sort}\n    end\n  end\n<\/code><\/pre>\n<ol>\n<li>\n<p>The error you are showing is a 500, not a 404. I don&#8217;t think this is a routing issue as much as it looks to be that something in your code is raising an unhandled exception.<\/p>\n<p>I&#8217;m also a little confused because calling Post.query() should be making a query that routes to your index method, not your show method.<\/p>\n<p>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?<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 01:34:33. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to list the contents of a post using Angular with Rails, but totally stuck. My controller: var myApp = angular.module(&#8216;myApp&#8217;, [&#8216;ngResource&#8217;]); myApp.controller(&#8216;categoryController&#8217;, [&#8216;$scope&#8217;, &#8216;Post&#8217;,function($scope, Post) { $scope.heading = &#8220;Hello from angular&#8221;; $scope.post = Post.query(); }]) myApp.factory(&#8216;Post&#8217;, [&#8216;$resource&#8217;, function($resource) { return $resource(&#8216;\/posts\/:id&#8217;, { id: &#8220;@id&#8221; } ); }]) Rails posts controller: def show @post [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1797","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1797"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1797\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}