正确生成表单构建器对象,同时丢弃 HTML,而不出现弃用警告
我有一个部分生成一个 div,其中包含一些表单字段。它使用作为输入提供的表单构建器变量“f”来正确命名参数中的字段(字段实际上是嵌套属性,因此名称类似于“[author][book][0][title]”) 。
我想在收到 AJAX 调用时使用相同的部分来根据新用户信息重新生成 div。我目前正在使用 <% form_for ... |f| %>
在我的 erb 文件中,但这会生成一条警告“<% %>”已弃用。
我的 erb 文件如下所示:
<% if f.nil? %>
<% form_for(@author, :id => :coupon_form) do |f| %>
<%= render "books_detail1", :f => f %>
<% end %>
<% else %>
<%= render "books_detail1", :f => f %>
<% end %>
那么在丢弃生成的 HTML 的同时创建表单生成器上下文的正确方法是什么?
I have a partial which generates a div with some form fields in it. It uses the form builder variable "f" which is provided as input to correctly name the fields in the parameter has (fields are actually nested attributes, so the name is like "[author][book][0][title]").
I want to use that same partial when receiving an AJAX call to regenerate the div based on new user information. I am currently using <% form_for ... |f| %>
in my erb file, but that generates a warning that "<% %>" is deprecated.
My erb file looks like the following:
<% if f.nil? %>
<% form_for(@author, :id => :coupon_form) do |f| %>
<%= render "books_detail1", :f => f %>
<% end %>
<% else %>
<%= render "books_detail1", :f => f %>
<% end %>
So what is the correct way to create a form builder context while discarding the generated HTML?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

正确的答案是使用fields_for。它生成相同的表单生成器对象,但不包含 html。我在子表单的使用中忘记了这一点,但它实际上是同一件事。
The correct answer is to use fields_for. It generates the same form builder object without the html. I lost track of this in it's use for sub-forms, but it's really the same thing.