Rails 3: Why an empty nested form generates a hidd

2019-04-15 14:35发布

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条回答
Ridiculous、
2楼-- · 2019-04-15 15:27

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.

查看更多
登录 后发表回答