【问题标题】:How to access a deep nested objects in Ruby on Rails?如何在 Ruby on Rails 中访问深层嵌套对象?
【发布时间】:2011-02-22 23:32:01
【问题描述】:

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

这是我的路线文件:

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

但是当我尝试访问用户的发表评论时,我得到一个错误... 我想选择与用户帖子相关的所有 cmets。这就是我所做的。 我的评论控制器:

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(未初始化的常量 发表::评论):
应用程序/控制器/cmets_controller.rb:7:in `索引' 这是相关的路线列表..

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'

【问题讨论】:

    标签: ruby-on-rails nested


    【解决方案1】:

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

    其实一切看起来都不错。

    【讨论】:

    • 与:@cmets = @post.cmets.all @post ||= @user.posts.find(params[:post_id])
    • Started GET "/users/1/posts/3/comments" for 127.0.0.1 at Tue Feb 22 15:53:16 +0100 2011 Processing by CommentsController#index as HTML Parameters: {"user_id"=&gt;"1", "spost_id"=&gt;"3"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE ("users"."id" = 1) LIMIT 1 Post Load (0.3ms) SELECT "posts".* FROM "posts" WHERE ("posts".user_id = 1) AND ("posts"."id" = 3) LIMIT 1 Completed in 21ms
    • @Reuben Mallaby,这是我的第一个想法,但这不是postuser 方法将返回@post@user 对象的原因。虽然错误指示第 7 行,但我们可以尝试在第 7 行附近播放:)。这其实是respond_with(@comments)方法
    • @fl00r 当我评论 respond_with 行时,它给出了同样的问题。 @Reuben Mallaby 当我用您的评论替换 @cmets = post.cmets.all 时,会出现同样的问题。
    • 为什么你的控制器有单数名称? CommentControllerPostController。它应该是多元化的。或者你只是打错了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-11
    • 2020-12-15
    • 1970-01-01
    • 2022-11-04
    • 1970-01-01
    相关资源
    最近更新 更多