Note that there are some explanatory texts on larger screens.

plurals
  1. POfields_for for has_many through relationship rails 3
    primarykey
    data
    text
    <p>I have problem in getting below code to work.</p> <pre><code> class Page &lt; ActiveRecord::Base has_many :page_parts, :through =&gt; :page_parts_pages has_many :page_parts_pages accepts_nested_attributes_for :page_parts, :allow_destroy =&gt; true accepts_nested_attributes_for :page_parts_pages, :allow_destroy =&gt; true end class PagePart &lt; ActiveRecord::Base has_many :page_parts_pages has_many :pages, :through =&gt; :page_parts_pages end class PagePartsPage &lt; ActiveRecord::Base belongs_to :page belongs_to :page_part end </code></pre> <p>Table Structure:-</p> <pre><code> pages id, title pages_parts id, title page_parts_pages id, page_id, page_part_id </code></pre> <p>View code</p> <pre><code> &lt;% page_fragment.each do |k,v| %&gt; &lt;% if v.nil? or v.blank? or v.empty? %&gt; &lt;% parts = f.object.page_parts.build if f.object.page_parts.blank? %&gt; &lt;%= f.fields_for :page_parts, parts do |p| %&gt; &lt;%= render 'page_part_form_field', :f =&gt; p %&gt; &lt;% end %&gt; &lt;% else %&gt; &lt;% parts_page = f.object.page_parts_pages.build if f.object.page_parts_pages.blank? %&gt; &lt;%= f.fields_for :page_parts_pages, parts_page do |p| %&gt; &lt;%= render 'page_part_page_form_field', :f =&gt; p %&gt; &lt;% end %&gt; &lt;% end %&gt; &lt;% end %&gt; </code></pre> <p>Actually the scenario is, I have to display the fields for page_parts and page_parts_pages on condition basis. If condition is satisfied, display fields for page_parts else display fields for page_parts_pages. </p> <p>It's working perfectly fine for new action but for edit action it is not displaying correctly. </p> <p>Any help is highly appreciated.</p> <p>Thanks in advance </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload