【发布时间】:2016-02-15 23:58:36
【问题描述】:
能否请更有经验的人告诉我我做错了什么?
我的 routes.rb 中有这个:
resources :interests do
resources :tweets, only: :index
end
在我的 TweetsController 我有这个索引方法:
def index
@tweets = get_user_tweets(params[:id])
respond_with(@tweets) do |format|
format.json { render :json => @tweets.as_json }
end end
计划是,当我去 /interests/:id/tweets 获取推文 json 格式时,这是我的 app.js 中的内容
app.factory('Tweets', ['$resource', function($resource) {
return $resource('/interests/:id/tweets.json')
} ]);
app.controller("HomeCtrl", ['$scope','Interest','Tweets', '$routeParams', function($scope,Interest,Tweets, $routeParams) {
$scope.interest = Interest.get({id: $routeParams.id})
$scope.tweets = Tweets.query();
$scope.works = "it works";
}]);
在配置中:
$routeProvider.when('/interests/:id/tweets',{
templateUrl: '/templates/tweets.html',
controller: 'HomeCtrl'
});
但是,例如当我转到interests/4/tweets 时,我收到消息“它有效”,但我没有收到推文,当我打开控制台时,它显示 /interests/tweets.json 404 ( Not Found) 这是错误的,因为它应该寻找 interests/:id/tweets
有人可以指出我正在犯的错误吗?
app.factory('Interests', ['$resource', function($resource) {
return $resource('/interests.json', {}, {
query: { method: 'GET', isArray: true },
create: { method: 'POST' }
})
}]);
app.factory('Interest', ['$resource', function($resource) {
return $resource('/interests/:id.json', {}, {
show: { method: 'GET' },
update: { method: 'PUT', params: {id: '@id'} },
delete: { method: 'DELETE', params: {id: '@id'} }
}
) }])
【问题讨论】:
-
它抛出这个错误是因为服务器认为 interests/tweets 中的“tweets”是一个 ID,所以它找不到一个值为“tweets”的 ID,但我不知道为什么当我给它 interests/:id/tweets 请帮助 时它是否正在搜索该 url
-
你能把
Interest的声明贴在你的HomeCtrl吗? -
这里,我添加了兴趣,谢谢你的时间
标签: javascript ruby-on-rails json angularjs twitter