茧 nested_form 渲染自己
Cocoon nested_form render yourself
我正在尝试创建一个用 cocoon 调用自身的表单,问题 has_many 个问题,正在生成无限循环:(
型号
class Question < ActiveRecord::Base
has_many :questions,:foreign_key => "parent_id", :dependent =>:destroy
belongs_to :basic_component
attr_accessible :description, :questions_attributes, :questions
end
我的问题_form
<%= semantic_form_for [:admin, @question] do |f| %>
<%= f.inputs do %>
<%= f.input :description %>
<div class="questions">
<%= f.semantic_fields_for :questions do |question| %>
<%= render 'question_fields', :f => question %>
<% end %>
<div class="links">
<%= link_to_add_association("Nova Pergunta", f, :questions, class: 'button') %>
</div>
</div>
<% end %>
<%= f.actions %>
<% end %>
我的_question_fields
<div class="nested-fields">
<%= f.inputs do %>
<%= f.input :description} %>
<div class="questions">
<%= f.semantic_fields_for :questions do |question| %>
<%= render 'question_fields', :f => question %>
<% end %>
<div class="links">
<%= link_to_add_association("Nova Pergunta", f, :questions, class: 'button') %>
</div>
</div>
<% end %>
</div>
无限循环:(,如何解决?
Rendered admin/questions/_question_fields.html.erb (168.4ms)
Rendered admin/questions/_question_fields.html.erb (376.2ms)
Rendered admin/questions/_question_fields.html.erb (586.4ms)
Rendered admin/questions/_question_fields.html.erb (780.2ms)
在您的问题字段中,您有:
<%= f.semantic_fields_for :questions do |question| %>
<%= render 'question_fields', :f => question %>
<% end %>
这是导致循环的原因。因为你在文件中,所以你一遍又一遍地调用这个文件。
link_to_add_association
也会在服务器端预渲染嵌套表单,所以当 link 被点击时,它可以插入 "new" 项。
这就是你的无限循环的来源:link_to_add_association
呈现嵌套形式,它也呈现嵌套形式,link_to_add_association
也呈现......无限;)
如果您真的希望能够构建一棵 indefinite/unlimited 树,cocoon 不适合您。您将不得不求助于 ajax.
但是,如果您可以限制最大深度,则可以很容易地向视图添加一个额外参数,如果级别低于您的最大级别,该参数只会呈现 link_to_add_association
。
这在 cocoon 问题之前已经出现过,可能的解决方案是 demonstrated
简而言之,假设最大深度为 5,您可以执行以下操作(为了便于阅读):
= semantic_form_for [:admin, @question] do |f|
= f.inputs do
= f.input :description
.questions
= f.semantic_fields_for :questions do |question|
= render 'question_fields', f: question, depth: 0
.links
= link_to_add_association "Nova Pergunta", f, :questions,
class: 'button', render_options: {locals: {depth: 0}}
= f.actions
和你的 question_fields
部分然后测试这个 depth
(并传播它)
.nested-fields
= f.inputs do
= f.input :description
.questions
= f.semantic_fields_for :questions do |question|
= render 'question_fields', :f => question, depth: depth + 1
- if depth < 5
.links
= link_to_add_association "Nova Pergunta", f, :questions,
class: 'button', render_options: {locals: {depth: depth + 1}}
我正在尝试创建一个用 cocoon 调用自身的表单,问题 has_many 个问题,正在生成无限循环:(
型号
class Question < ActiveRecord::Base
has_many :questions,:foreign_key => "parent_id", :dependent =>:destroy
belongs_to :basic_component
attr_accessible :description, :questions_attributes, :questions
end
我的问题_form
<%= semantic_form_for [:admin, @question] do |f| %>
<%= f.inputs do %>
<%= f.input :description %>
<div class="questions">
<%= f.semantic_fields_for :questions do |question| %>
<%= render 'question_fields', :f => question %>
<% end %>
<div class="links">
<%= link_to_add_association("Nova Pergunta", f, :questions, class: 'button') %>
</div>
</div>
<% end %>
<%= f.actions %>
<% end %>
我的_question_fields
<div class="nested-fields">
<%= f.inputs do %>
<%= f.input :description} %>
<div class="questions">
<%= f.semantic_fields_for :questions do |question| %>
<%= render 'question_fields', :f => question %>
<% end %>
<div class="links">
<%= link_to_add_association("Nova Pergunta", f, :questions, class: 'button') %>
</div>
</div>
<% end %>
</div>
无限循环:(,如何解决?
Rendered admin/questions/_question_fields.html.erb (168.4ms)
Rendered admin/questions/_question_fields.html.erb (376.2ms)
Rendered admin/questions/_question_fields.html.erb (586.4ms)
Rendered admin/questions/_question_fields.html.erb (780.2ms)
在您的问题字段中,您有:
<%= f.semantic_fields_for :questions do |question| %>
<%= render 'question_fields', :f => question %>
<% end %>
这是导致循环的原因。因为你在文件中,所以你一遍又一遍地调用这个文件。
link_to_add_association
也会在服务器端预渲染嵌套表单,所以当 link 被点击时,它可以插入 "new" 项。
这就是你的无限循环的来源:link_to_add_association
呈现嵌套形式,它也呈现嵌套形式,link_to_add_association
也呈现......无限;)
如果您真的希望能够构建一棵 indefinite/unlimited 树,cocoon 不适合您。您将不得不求助于 ajax.
但是,如果您可以限制最大深度,则可以很容易地向视图添加一个额外参数,如果级别低于您的最大级别,该参数只会呈现 link_to_add_association
。
这在 cocoon 问题之前已经出现过,可能的解决方案是 demonstrated
简而言之,假设最大深度为 5,您可以执行以下操作(为了便于阅读):
= semantic_form_for [:admin, @question] do |f|
= f.inputs do
= f.input :description
.questions
= f.semantic_fields_for :questions do |question|
= render 'question_fields', f: question, depth: 0
.links
= link_to_add_association "Nova Pergunta", f, :questions,
class: 'button', render_options: {locals: {depth: 0}}
= f.actions
和你的 question_fields
部分然后测试这个 depth
(并传播它)
.nested-fields
= f.inputs do
= f.input :description
.questions
= f.semantic_fields_for :questions do |question|
= render 'question_fields', :f => question, depth: depth + 1
- if depth < 5
.links
= link_to_add_association "Nova Pergunta", f, :questions,
class: 'button', render_options: {locals: {depth: depth + 1}}