Rails 3: Why an empty nested form generates a hidden input field?

喜夏-厌秋 提交于 2019-12-09 23:47:26

问题


Why this:

# 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">

?

Relevant controller code:

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

回答1:


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.



来源:https://stackoverflow.com/questions/4488518/rails-3-why-an-empty-nested-form-generates-a-hidden-input-field

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!