Rails 3:为什么一个空的嵌套表单会生成一个隐藏的输入字段? [英] Rails 3: Why an empty nested form generates a hidden input field?

查看:72
本文介绍了Rails 3:为什么一个空的嵌套表单会生成一个隐藏的输入字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么这样做:

# edit.html.erb
<%= form_for @product do |f| %>
  <%= f.fields_for :shop do |sf| %>
    # Nothing here
  <% end %>
<% end %>

生成一个隐藏的输入字段:

generates a hidden input field:

<input type="hidden" value="23" name="product[shop_attributes][id]" id="product_shop_attributes_id">

?

相关控制器代码:

def edit
  @product = Product.find(params[:id])
end

推荐答案

这是因为您正在编辑的@product拥有一家商店. Rails已将其插入fields_for中,以便在提交表单时,它便知道这些嵌套属性用于哪家商店.这是默认的嵌套属性行为.

It'll be because the @product you're editing has a shop. Rails has inserted that in the fields_for so that when the form is submitted, it knows which shop those nested attributes are for. It's default nested attributes behaviour.

这篇关于Rails 3:为什么一个空的嵌套表单会生成一个隐藏的输入字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆