Ruby on rails ';显示';带角度和轨道的动作
我试图用Angular和Rails列出一篇文章的内容,但完全卡住了 我的控制器:Ruby on rails ';显示';带角度和轨道的动作,ruby-on-rails,angularjs,Ruby On Rails,Angularjs,我试图用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()
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" } );
}])
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html
format.json { render json: @post }
end
end
Rails posts控制器:
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" } );
}])
def show
@post = Post.find(params[:id])
respond_to do |format|
format.html
format.json { render json: @post }
end
end
“Hello from angular”显示得很好,但是其余部分给了我一个错误:
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
您显示的错误是500,而不是404。我不认为这是一个路由问题,因为代码中的某些内容引发了未处理的异常 我也有点困惑,因为调用Post.query()应该生成一个路由到index方法的查询,而不是show方法 如果您试图路由到show方法,您可能希望执行Post.get({id:someID}),否则,您的索引方法是什么样子的?query()方法发送一个
get
请求,而不传递Post id。这就是为什么您的URL不完整,并且会出现500个错误
定义您的工厂,就像在中完成一样,然后调用
$scope.post = Post.show(id);
你能把
routes.rb的内容贴出来吗?@zeantsoi当然可以!我没有索引方法。我只是在尝试Angular,并试图只显示单独的显示方法。@DenisG您是否尝试将Post.query()更改为调用get()以传递一些ID?