Monday 15 June 2015

RAILS: Calling a parent model within a nested partial form -


I am trying to call a variable from the 'product' model in the 'variant' partial. I 'variant' I can call using existing variable from:
  <% = f.object.product_id%>   

But I can not get it to work:

  <% = f.object.product.id%>   

I can do this:

  & lt;% = f.object.product% & gt;   

But this # & lt; Product: 0x007fee2c9a8ec8 & gt; and I do not know what to do with it!

Here is a detailed overview of the situation:

I have a 'store' model which is 'many' products 'each product' has_many 'variants' variants are nested here. /store/show.html.erb 'is the file:

  <% @ store.products.each do | Products | & Gt%; & Lt; TR & gt; & Lt; TD & gt; Do & lt;% = form_for (Products) | F | & Gt%; & Lt;% = f.fields_for: Variants | Builder | & Gt%; & Lt;% = Render 'variant_fields', f: Builder% & gt; & Lt;% end% & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt;% end% & gt;   

And here '/ store / _variant_fields' is partial:

  & lt; Fieldet & gt; & Lt; / Table & gt; & Lt; Tbody & gt; & Lt; TR & gt; & Lt; Td> & Lt;% = f.label: variant_name% & gt; & Lt;% = f.text_field: variant_name% & gt; & Lt; / Td> & Lt; / TR & gt; & Lt; / Tbody & gt; & Lt; / Table & gt; & Lt; / Fieldset & gt;    

The easiest solution is to pass the product to you partially:

  & lt;% render 'variant_fields', f: builder, product: f.object% & gt;   

In your part:

  & lt; Fieldet & gt; & Lt; / Table & gt; & Lt; Tbody & gt; & Lt; TR & gt; & Lt; Td> & Lt;% = f.label: variant_name% & gt; & Lt;% = f.text_field: variant_name% & gt; & Lt;% = product.inspect% & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / Tbody & gt; & Lt; / Table & gt; & Lt; / Fieldset & gt;   

See what product.inspect shows.

No comments:

Post a Comment