html - Issue with Nested Form rendering -
i have rails 4 app has form looks like:
<%= form_for @store, :html => {:multipart => true} |f| %> <%= f.fields_for :products |product| %> <%= render partial: "edit_product_fields", locals: {product:product} %> <% end %> <%= f.submit %> <% end %> and edit_product_fields partial looks like:
<select> <option value="textbook" <% if product.type_of == "textbook" %>selected<% end %>>textbook</option> <option value="magazine" <% if product.type_of == "magazine" %>selected<% end %>>magazine</option> <option value="book" <% if product.type_of == "book" %>selected<% end %>>book</option> </select> when have code this, error:
undefined method `type_of' #<nestedform::builder:0x00000102304f78> but if change render line (i made less explicit taking out partial:):
<%= render "edit_product_fields", locals: {product:product} %> i error:
undefined local variable or method `product' #<#<class:0x0000010235a248>:0x0000010684b3c0>
in first code block, have builder object being stored product.
fortunately, builder provides object method can access actual object it's representing in form:
<select> <option value="textbook" <%= 'selected' if product.object.type_of == "textbook" %>>textbook</option> <option value="magazine" <%= 'selected' if product.object.type_of == "magazine" %>>magazine</option> <option value="book" <%= 'selected' if product.object.type_of == "book" %>>book</option> </select>
Comments
Post a Comment