我有一个带有has_many的rails应用程序:通过这样的模型:
class Blog < ActiveRecord::Base
has_many :blog_categorizations, dependent: :destroy
has_many :categories, :through => :blog_categorizations
accepts_nested_attributes_for :categories, allow_destroy: true
end
class Category < ActiveRecord::Base
has_many :blog_categorizations, dependent: :destroy
has_many :blog, :through => :blog_categorizations
accepts_nested_attributes_for :blog, allow_destroy: true
end
class BlogCategorization < ActiveRecord::Base
belongs_to :blog
belongs_to :category
end
在我的Rails管理配置中,我有:
config.model Blog do
edit do
field :title
field :content
field :source
field :author
field :date
field :photos
field :categories
end
end
现在,当我尝试通过rails_admin添加新博客,或尝试编辑预先存在的博客时,我得到了无限渲染的rails_admin模板,如下所示:
Started GET "/admin/recipe/new?_pjax=%5Bdata-pjax-container%5D" for 127.0.0.1 at 2014-10-29 07:41:35 -0700
ActiveRecord::SchemaMigration Load (0.8ms) SELECT "schema_migrations".* FROM "schema_migrations"
Processing by RailsAdmin::MainController#new as HTML
Parameters: {"_pjax"=>"[data-pjax-container]", "model_name"=>"recipe"}
User Load (1.0ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (2.3ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_text.html.haml (1.0ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_datetime.html.haml (1.7ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (25.6ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (16.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_submit_buttons.html.haml (2.9ms)
(0.6ms) SELECT COUNT(*) FROM "blogs"
Recipe Load (0.7ms) SELECT "blogs".* FROM "blogs" ORDER BY blogs.id desc
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_filtering_select.html.haml (14.3ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_file_upload.html.haml (4.0ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.3ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (1.6ms)
(0.4ms) SELECT COUNT(*) FROM "blog_categorizations"
BlogCategorization Load (0.3ms) SELECT "blog_categorizations".* FROM "blog_categorizations" ORDER BY blog_categorizations.id desc
Category Load (0.5ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 5)
Recipe Load (0.3ms) SELECT "blogs".* FROM "blogs" WHERE "blogs"."id" IN (65)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_filtering_multiselect.html.haml (22.1ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.6ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_text.html.haml (0.3ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_datetime.html.haml (0.9ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.3ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.2ms)
CACHE (0.0ms) SELECT "blogs".* FROM "blogs" ORDER BY blogs.id desc
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_filtering_select.html.haml (4.0ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_file_upload.html.haml (0.5ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
CACHE (0.0ms) SELECT "blog_categorizations".* FROM "blog_categorizations" ORDER BY blog_categorizations.id desc
CACHE (0.0ms) SELECT "categories".* FROM "categories" WHERE "categories"."id" IN (1, 5)
CACHE (0.0ms) SELECT "blogs".* FROM "blogs" WHERE "blogs"."id" IN (65)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_filtering_multiselect.html.haml (3.9ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.1ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_text.html.haml (0.3ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_field.html.haml (0.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_datetime.html.haml (1.0ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.2ms)
Rendered /Users/adam419/.rbenv/versions/2.0.0-p481/lib/ruby/gems/2.0.0/gems/rails_admin-0.6.3/app/views/rails_admin/main/_form_nested_many.html.haml (1.2ms)
同样的输出只是无限发生并且似乎没有停止,几乎就像它进入了加载博客类别,类别博客等的无限循环一样。
如何阻止这种情况发生?
答案 0 :(得分:0)
删除accepts_nested_attributes_for:Category.rb中的博客解决了问题
accepts_nested_attributes_for :blog
以前是在Category.rb中,但在发布的代码中删除了。
另外,离开
accepts_nested_attributes_for :blog
但为博客模型添加了inverse_of:category,如
has_many :blog, :through => :blog_categorizations, inverse_of: category
和
has_many :blog, :through => :blog_categorizations, inverse_of: blog
导致问题消失。我目前正在使用inverse_of选项,即使它不适用于:通过模型,但出于某种原因,Rails管理员可以在多对多中使用。