开发者

How to access a deep nested objects in Ruby on Rails?

开发者 https://www.devze.com 2023-02-12 08:52 出处:网络
Hello i am facing a problem with accessing objects that are nested too deep. I\'m hoping one of you can help me out or faced the same issue? Because of my application navigation I can\'t re-factor my

Hello i am facing a problem with accessing objects that are nested too deep. I'm hoping one of you can help me out or faced the same issue? Because of my application navigation I can't re-factor my nesting objects in a more simple way because i always need to use the root object for example "Users"

This is my route file:

resources :users do
    resources :posts do
      resources :comments
    end
end

User model

class User < ActiveRecord::Base
  has_many :posts
end

Post Model

class Post < ActiveRecord::Base
   belongs_to :user
  has_many :comments
end

Comment Model

class Comment < ActiveRecord::Base
  belongs_to :post
end

Now when i try to access user's posts it goes all ok. It allows me to select User's posts all ok..

class PostsController < ApplicationController
  before_filter :user
  respond_to :html, :xml

  def index
    @posts = user.posts.all
    respond_with(@posts)
  end

 protected
  def user
    @user ||= User.find(params[:user_id])
  end
end

But when i try to access the User's Post Comment's i get an error... I want to select all comments that are related to the User's posts.. So here is what i did. My Comments Controller:

class CommentsController < ApplicationController
   before_filter :user, :post
   respond_to :html, :xml

  def index
    @comments = post.comments.all
    respond_with(@comments)
  end
  protected
  def user
    @user ||= User.find(params[:user_id])
  end

  def post
    @post ||= user.posts.find(params[:post_id])
  end  
end

The following error occurs when trying to access for example: http://localhost:3000/users/1/posts/3/comments/

NameError (uninitialized constant Post::Comments):

app/controllers/comments_controller.rb:7:in `index' This is the route list that is relevant..

user_post_comments_index GET    /users/:user_id/posts/:post_id/comments(.:format)          {:action=>"index", :controller=>"comments"}
                           POST   /users/:user_id/posts/:post_id/comments(.:format)          {:action=>"create", :controller=>"comments"}
  new_user_post_comments GET    /users/:user_id/posts/:post_id/comments/new(.:format)      {:action=>"new", :controller=>"comments"}
 edit_user_post_comments GET    /users/:user_id/posts/:post_id/comments/:id/edit(.:format) {:action=>"edit", :controller=>"comments"}
      user_post_comments GET    /users/:user_id/posts/:post_id/comments/:id(.:format)      {:action=>"show", :controller=>"comments"}
                           PUT    /users/:user_id/posts/:post_id/comments/:id(.:format)      {:action=>"update", :controller=>"comments"}
                           DELETE /users/:user_id/posts/:post_id/comments/:id(.:format)      {:action=>"destroy", :controller=>"comments"}
           user_posts GET    /users/:user_id/posts(.:format)                           {:action=>"index", :controller=>"posts"}
                           POST   /users/:user_id/posts(.:format)                           {:action=>"create", :controller=>"posts"}
         new_user_post GET    /users/:user_id/posts/new(.:format)                       {:action=>"new", :controller=>"posts"}
        edit_user_post GET    /users/:u开发者_如何学运维ser_id/posts/:id/edit(.:format)                  {:action=>"edit", :controller=>"posts"}
             user_post GET    /users/:user_id/posts/:id(.:format)                       {:action=>"show", :controller=>"posts"}

Not sure if this can help but i've added a bit of the debug messages..

activerecord (3.0.3) lib/active_record/base.rb:1199:in `compute_type'
activerecord (3.0.3) lib/active_record/reflection.rb:162:in `send'
activerecord (3.0.3) lib/active_record/reflection.rb:162:in `klass'
activerecord (3.0.3) lib/active_record/reflection.rb:198:in `quoted_table_name'
activerecord (3.0.3) lib/active_record/associations/has_many_association.rb:102:in `construct_sql'
activerecord (3.0.3) lib/active_record/associations/association_collection.rb:24:in `initialize'
activerecord (3.0.3) lib/active_record/associations/has_many_association.rb:11:in `initialize'
activerecord (3.0.3) lib/active_record/associations.rb:1492:in `new'
activerecord (3.0.3) lib/active_record/associations.rb:1492:in `comments'
actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
actionpack (3.0.3) lib/abstract_controller/base.rb:151:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/rendering.rb:11:in `process_action'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:18:in `process_action'
activesupport (3.0.3) lib/active_support/callbacks.rb:450:in `_run__677074153__process_action__199225275__callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `send'
activesupport (3.0.3) lib/active_support/callbacks.rb:409:in `_run_process_action_callbacks'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `send'
activesupport (3.0.3) lib/active_support/callbacks.rb:93:in `run_callbacks'
actionpack (3.0.3) lib/abstract_controller/callbacks.rb:17:in `process_action'
actionpack (3.0.3) lib/action_controller/metal/instrumentation.rb:30:in `process_action'


Just for debug: remove respond_with(@comments) from index action

Actually everythings looks good.

0

精彩评论

暂无评论...
验证码 换一张
取 消