我有一个连接到REST rails应用程序的应用程序。我有两个资源:category
和post
;帖子嵌套在类别中。
我能够成功地对 CRUD 类别进行分类。我只能列出帖子,但在显示或更新方面没有成功。
这是我的示例代码:
config/routes.rb:
resources :categories do
resources :posts
end
resources :posts do
resources :comments
end
resources :comments
models/post.rb:
class Post < Connector
self.site += "/categories/:category_id"
end
控制器/posts_controller.rb:
class PostsController < ApplicationController
def index
@category = Category.find(params[:category_id])
@posts = Post.all(:params => {:category_id => @category.id})
respond_to do |format|
format.html
format.xml { render :xml => @posts }
end
end
def show
@category = Category.find(params[:category_id])
@post = Post.find(:id, :params => {:category_id => @category.id})
respond_to do |format|
format.html
format.xml { render :xml => @post }
end
end
end
耙子路线:
category_posts GET /categories/:category_id/posts(.:format) {:action=>"index", :controller=>"posts"}
POST /categories/:category_id/posts(.:format) {:action=>"create", :controller=>"posts"}
new_category_post GET /categories/:category_id/posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_category_post GET /categories/:category_id/posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
category_post GET /categories/:category_id/posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /categories/:category_id/posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /categories/:category_id/posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
categories GET /categories(.:format) {:action=>"index", :controller=>"categories"}
POST /categories(.:format) {:action=>"create", :controller=>"categories"}
new_category GET /categories/new(.:format) {:action=>"new", :controller=>"categories"}
edit_category GET /categories/:id/edit(.:format) {:action=>"edit", :controller=>"categories"}
category GET /categories/:id(.:format) {:action=>"show", :controller=>"categories"}
PUT /categories/:id(.:format) {:action=>"update", :controller=>"categories"}
DELETE /categories/:id(.:format) {:action=>"destroy", :controller=>"categories"}
post_comments GET /posts/:post_id/comments(.:format) {:action=>"index", :controller=>"comments"}
POST /posts/:post_id/comments(.:format) {:action=>"create", :controller=>"comments"}
new_post_comment GET /posts/:post_id/comments/new(.:format) {:action=>"new", :controller=>"comments"}
edit_post_comment GET /posts/:post_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
post_comment GET /posts/:post_id/comments/:id(.:format) {:action=>"show", :controller=>"comments"}
PUT /posts/:post_id/comments/:id(.:format) {:action=>"update", :controller=>"comments"}
DELETE /posts/:post_id/comments/:id(.:format) {:action=>"destroy", :controller=>"comments"}
posts GET /posts(.:format) {:action=>"index", :controller=>"posts"}
POST /posts(.:format) {:action=>"create", :controller=>"posts"}
new_post GET /posts/new(.:format) {:action=>"new", :controller=>"posts"}
edit_post GET /posts/:id/edit(.:format) {:action=>"edit", :controller=>"posts"}
post GET /posts/:id(.:format) {:action=>"show", :controller=>"posts"}
PUT /posts/:id(.:format) {:action=>"update", :controller=>"posts"}
DELETE /posts/:id(.:format) {:action=>"destroy", :controller=>"posts"}
索引工作正常,我可以列出帖子。
如果我在控制台中这样做:
Post.find(3, :params=>{:category_id=>2})
我能够获得所需的响应,但从浏览器中我收到以下错误。
Failed. Response code = 404. Response message = Not Found.
尝试将路由更新为
resources :categories do
resources :posts do
resources :comments
end
end
然后,您应该能够将它们作为嵌套资源进行访问。 不确定< Connector
的去向,Connector
是基于ActiveModel的模型吗?
<subjective>
此外,如果你嵌套了多个级别,你真的应该重新审视你的应用程序设计,因为这很快就会成为一个令人头疼的问题,请参阅:http://weblog.jamisbuck.org/2007/2/5/nesting-resources(此代码是rails 2.x风格,但设计问题仍然存在) </subjective