2013-11-25 17 views
0

我想列出一個帖子的內容使用Angular與Rails,但完全卡住。'顯示'與Angular和Rails的動作

我的控制器:

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的帖子控制器:

def show 
    @post = Post.find(params[:id]) 

    respond_to do |format| 
     format.html 
     format.json { render json: @post } 
    end 
    end 

'你好,從角' 是顯示了罰款,但剩下的給我一個錯誤:

GET http://localhost:3000/posts/show 500 (Internal Server Error) 

找不到找出這個壞男孩的正確方法。 謝謝你的任何有用的輸入

編輯:routes.rb中

resources :users 

    resources :hubs, shallow: true do 
    resources :posts, shallow: true 
    resources :comments, shallow: true do 
     collection {post :sort} 
    end 
    end 
+0

你可以張貼'routes.rb'的內容是什麼? – zeantsoi

+0

@zeantsoi肯定的事情!更新 –

回答

1

你是顯示錯誤是500,而不是404,我不認爲這是一個路由的問題,它更看起來是代碼中的東西引發了未處理的異常。

我也有點困惑,因爲調用Post.query()應作出查詢路線索引方法,而不是你的節目的方法。

如果你正在嘗試的路線,你可能想要做Post.get表演方法({ID:someID}),否則什麼索引方法是什麼樣子?

+0

以及我沒有索引方法。我只是在嘗試Angular,並試圖僅展示單獨的展示方法。 –

+0

@DenisG你嘗試改變你Post.query()的調用get()傳遞一些ID? – mfollett

0

query()方法發送一個請求GET不經過郵政編號。這就是爲什麼你的網址不完整,你會得到500錯誤。

像它在this article的完成定義你的工廠,然後調用

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