问题
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