如何在 Ruby on Rails 中访问深层嵌套对象?
您好,我面临访问嵌套太深的对象的问题。我希望你们中的一个人可以帮助我或遇到同样的问题?由于我的应用程序导航,我无法以更简单的方式重构我的嵌套对象,因为我总是需要使用根对象,例如“用户”
这是我的路线文件:
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(未初始化常量 帖子::评论):
应用程序/控制器/comments_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'
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/:user_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'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅用于调试:从索引操作中删除
respond_with(@comments)
实际上,一切看起来都不错。
Just for debug: remove
respond_with(@comments)
from index actionActually everythings looks good.