繁体   English   中英

如何在Ruby on Rails中访问深层嵌套的对象?

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

您好,我在访问嵌套太深的对象时遇到问题。 我希望你们中的一个可以帮助我或遇到同样的问题? 由于我的应用程序导航,我无法以更简单的方式重构嵌套对象,因为我总是需要使用根对象,例如“ Users”

这是我的路线文件:

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

用户模型

class User < ActiveRecord::Base
  has_many :posts
end

邮政模型

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

评论模型

class Comment < ActiveRecord::Base
  belongs_to :post
end

现在,当我尝试访问用户的帖子时,一切正常。 它可以让我选择所有用户的帖子。

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

但是,当我尝试访问用户的帖子评论时,出现错误...我想选择与用户的帖子相关的所有评论。所以这就是我所做的。 我的评论控制器:

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

例如,尝试访问时发生以下错误: http:// localhost:3000 / users / 1 / posts / 3 / comments /

NameError(未初始化的常量Post :: Comments):
app / controllers / comments_controller.rb:7:in'index'这是相关的路由列表。

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/:user_id/posts/:id/edit(.:format)                  {:action=>"edit", :controller=>"posts"}
             user_post GET    /users/:user_id/posts/:id(.:format)                       {:action=>"show", :controller=>"posts"}

不知道这是否可以帮助,但是我添加了一些调试消息。

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'

仅用于调试:从索引操作中删除respond_with(@comments)

其实一切看起来都不错。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM