对于具有空自引用关联的模型,rails 渲染操作两次

发布于 2024-10-01 15:49:03 字数 1913 浏览 0 评论 0原文

我对这个感到非常困惑。我怀疑是否有人能立即告诉我问题是什么,但是任何建议调试和调查此问题的方法的答案将不胜感激——我尝试将一些调试调用放入控制器中,但在遵循路径方面运气不佳通过 Rails 的内部,因此指向源代码中要调查的位置的指针会很棒。

在 Ruby 1.8.7/Rails 2.3.5 和 Ruby 1.9.2/Rails 3.0.1 下都会发生这种情况。

我有以下模型:

class Collection < ActiveRecord::Base
  has_one :collection_profile, :dependent => :destroy

  belongs_to :parent, :class_name => "Collection"
  has_many :children, :class_name => "Collection", :foreign_key => "parent_id"
end

具有以下路线:

resources :collections do
  resource  :collection_profile
  resources :collections
end

还有一些验证可确保它们不能嵌套超过一个深度 - 即,一个集合可以有一个父集合,或者它可以有子集合,但不能同时有两者。

如果一个集合没有子集合——即,它要么是一个子集合,要么是一个独立的集合——那么该集合下的任何操作都会渲染两次。换句话说:

为有子项的集合重新加载显示页面一次:

Started GET "/collections/TheFirstCollection" for 127.0.0.1 at 2010-11-09 20:55:07 -0500
...
Rendered collections/show.html.erb within layouts/application (1708.9ms)
Completed 200 OK in 1784ms (Views: 1706.0ms | ActiveRecord: 34.3ms)

为没有子项的集合重新加载显示页面一次:

Started GET "/collections/SecondCollection" for 127.0.0.1 at 2010-11-09 20:56:48 -0500
...
Rendered collections/show.html.erb within layouts/application (637.3ms)
Completed 200 OK in 921ms (Views: 654.6ms | ActiveRecord: 13.7ms)

Started GET "/collections/SecondCollection" for 127.0.0.1 at 2010-11-09 20:56:55 -0500
...
Rendered collections/show.html.erb within layouts/application (828.6ms)
Completed 200 OK in 906ms (Views: 843.8ms | ActiveRecord: 14.9ms)

对于所有其他嵌套资源(其中有很多),都会发生同样的事情 - 例如 /collections/[collection name]/profile、/collections/[集合名称]/works、/collections/[集合名称]/people -- 如果集合有子集合,则所有操作都会渲染一次并返回。否则,它们会渲染两次。

注意——浏览器显示第一次渲染的结果,但浏览器状态栏仍然显示“正在等待”,直到第二次渲染完成,但它不会重绘屏幕,至少在 Firefox 或 Safari 中是这样。

我创建了一个具有相同结构的测试应用程序(对模型使用相同的名称“集合”,以防万一这是问题的根源),并且错误不会重复。

就像我上面说的——任何想法,甚至如何调试它,都将受到极大的赞赏。

I am pretty baffled by this one. I doubt anyone can tell me right off the bat what the issue is, but any answers suggesting ways of debugging and investigating this would be much appreciated -- I've tried putting in some debugging calls into the controller without much luck in following the path through the guts of Rails, so a pointer to where in the source to investigate would be great.

This occurs under both Ruby 1.8.7/Rails 2.3.5 and Ruby 1.9.2/Rails 3.0.1.

I have the following model:

class Collection < ActiveRecord::Base
  has_one :collection_profile, :dependent => :destroy

  belongs_to :parent, :class_name => "Collection"
  has_many :children, :class_name => "Collection", :foreign_key => "parent_id"
end

with the following route:

resources :collections do
  resource  :collection_profile
  resources :collections
end

There's also some validation that ensures that these can't be nested more than one deep -- ie, a Collection can have a parent collection OR it can have children collections, not both.

If a Collection does not have children -- ie, either it is a subcollection or it is a standalone collection -- then any actions under that collection render twice. In other words:

Reloading show page once for a Collection with children:

Started GET "/collections/TheFirstCollection" for 127.0.0.1 at 2010-11-09 20:55:07 -0500
...
Rendered collections/show.html.erb within layouts/application (1708.9ms)
Completed 200 OK in 1784ms (Views: 1706.0ms | ActiveRecord: 34.3ms)

Reloading show page once for a Collection without children:

Started GET "/collections/SecondCollection" for 127.0.0.1 at 2010-11-09 20:56:48 -0500
...
Rendered collections/show.html.erb within layouts/application (637.3ms)
Completed 200 OK in 921ms (Views: 654.6ms | ActiveRecord: 13.7ms)

Started GET "/collections/SecondCollection" for 127.0.0.1 at 2010-11-09 20:56:55 -0500
...
Rendered collections/show.html.erb within layouts/application (828.6ms)
Completed 200 OK in 906ms (Views: 843.8ms | ActiveRecord: 14.9ms)

The same thing happens for all the other nested resources (of which there are a bunch) -- eg /collections/[collection name]/profile, /collections/[collection name]/works, /collections/[collection name]/people -- if the collection has children, the actions all render once and return. Otherwise, they render twice.

Note -- the browser displays the results of the first render, but the browser status bar still shows "waiting" until the second render completes, but it does not redraw the screen, at least in Firefox or Safari.

I've created a test app with the same structure (using the same name "collection" for the model in case that was the source of the wonkiness), and the error doesn't duplicate.

Like I said above -- any ideas even how to debug this would be hugely appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文