【发布时间】:2015-01-18 18:40:05
【问题描述】:
我查看了 StackOverflow 上的其他示例,但我仍然需要这方面的帮助。
下面我的问题是使用 o.get 函数通过其 id 检索单个帖子。
在调试时,我发现当我点击正确的链接时,我实际上是在点击这个功能。但是,单击时会引发406 Not Acceptable 错误。
我不确定问题是在角度方面,还是与我的表演动作有关,这也可以在下面找到。
这是我的服务的代码:
angular.module('hackerNews')
.factory('posts', ['$http',
function($http) {
var o = {
posts: []
};
o.getAll = function() {
return $http.get('/posts.json').success(function(data) {
angular.copy(data, o.posts);
});
};
o.create = function(post) {
return $http.post('/posts.json', post).success(function(data) {
o.posts.push(data);
});
};
o.upvote = function(post) {
return $http.put('/posts/' + post.id + '/upvote.json').success(function(data) {
post.upvotes += 1;
});
};
o.get = function(id) {
return $http.get('/posts/' + id).then(function(res) {
return res.data;
});
};
o.addComment = function(id, comment) {
return $http.post('/posts/' + id + '/comments.json', comment);
};
o.upvoteComment = function(post, comment) {
return $http.put('/posts/' + post.id + '/comments' + comment.id + '/upvote.json')
.success(function(data) {
comment.upvotes += 1;
});
};
return o;
}])
表演动作:
def show
respond_with Post.find(params[:id])
end
任何帮助将不胜感激。
谢谢!
编辑:
这是我的控制器中的内容:
这是我的应用程序控制器中的内容,我有以下内容:
respond_to :json
大部分控制器如下:
class PostsController < ApplicationController
def index
respond_with Post.all
end
def create
respond_with Post.create(post_params)
end
def show
respond_with Post.find(params[:id])
end
def upvote
post = Post.find(params[:id])
post.increment!(:upvotes)
respond_with post
end
private
def post_params
params.require(:post).permit(:link, :title)
end
end
【问题讨论】:
-
抱歉,我指的是 Angular 1.3.x,而不是 13.x
-
您是否使用 JSON 响应? 406 错误表示服务器的响应类型不在您请求的接受类型列表中。
-
你的控制器中有
respond_to :json吗? -
我用控制器中的内容更新了问题。
-
@basia。线程更新。我应该把respond_to :json 代码放在什么地方,在post 控制器的底部?
标签: javascript ruby-on-rails angularjs rest