I have an app which connects to a REST rails app. I have two resources: category and post; post is nested in category.
I was able to successfully CRUD categories. I am only able to list posts but no success in showing or updating.
Here is my sample code:
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
controllers/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
rake routes:
    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开发者_StackOverflow中文版(.: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"}
Index works fine, I am able to list posts.
If I do in in console:
Post.find(3, :params=>{:category_id=>2})
I am able to get the desired response but from the browser I get the following error.
Failed.  Response code = 404.  Response message = Not Found.
try updating your routes to be
resources :categories do
    resources :posts do
      resources :comments
    end
  end
then you should be able to access them as nested resources.  Not sure where the < Connector is going, is Connector a model based on ActiveModel?
<subjective>
Also, if you are nesting more than one level, you should really take a second look at your application design, as this quickly becomes a headache, see: http://weblog.jamisbuck.org/2007/2/5/nesting-resources  (this code is rails 2.x style, but the design issues remain)
</subjective
                                        
                                        
                                        
                                        
                                        
                                        
                                        
                                        
 加载中,请稍侯......
      
精彩评论