我正在尝试在创建新条目时干掉我的某个表单,这些表单有时会有其他字段(对于嵌套模型),但在执行更新时不需要这些字段。因此,我试图通过一个块传递其他字段,但表单对象没有正确传递。
有没有办法将表单对象(或任何变量)传递给yield
?
参考示例代码:
_form.slim
= nested_form_for @model do |f|
.row
= f.label :name
= f.text_field :name, autofocus: true
...
= yield
...
= f.submit 'Save'
new.html.slim
== render layout: 'model/form' do
h3 Additional Fields
= f.fields_for :nested do |h|
= a.label :name, 'Nested Name'
= a.text_field :name
= a.link_to_remove do
= fa_icon 'times-circle-o'
= f.link_to_add "Add another nested model", :nested
edit.html.slim
== render layout: 'model/form'
答案 0 :(得分:1)
详细说明我的评论,这就是我使用partials的方式:
<强> _form.slim 强>
= nested_form_for @model do |f|
.row
= f.label :name
= f.text_field :name, autofocus: true
...
- if defined?(additional_fields)
h3 Additional Fields
= f.fields_for :nested do |h|
= a.label :name, 'Nested Name'
= a.text_field :name
= a.link_to_remove do
= fa_icon 'times-circle-o'
= f.link_to_add "Add another nested model", :nested
...
= f.submit 'Save'
<强> new.html.slim 强>
== render 'model/form', :additional_fields => true
<强> edit.html.slim 强>
== render 'model/form'
我可能会遗漏一些东西,但我不确定为什么这不起作用。