0

I have problem in getting below code to work.

  class Page < ActiveRecord::Base
     has_many :page_parts, :through => :page_parts_pages
     has_many :page_parts_pages
     accepts_nested_attributes_for :page_parts, :allow_destroy => true
     accepts_nested_attributes_for :page_parts_pages, :allow_destroy => true
  end

  class PagePart < ActiveRecord::Base
     has_many :page_parts_pages
     has_many :pages, :through => :page_parts_pages
  end

  class PagePartsPage < ActiveRecord::Base
     belongs_to :page
     belongs_to :page_part
  end

Table Structure:-

  pages
      id, title

  pages_parts
      id, title

  page_parts_pages
      id, page_id, page_part_id

View code

  <% page_fragment.each do |k,v| %>

     <%  if v.nil? or v.blank? or v.empty? %>

       <% parts =  f.object.page_parts.build if f.object.page_parts.blank? %>

       <%= f.fields_for :page_parts, parts do |p| %>

         <%= render 'page_part_form_field', :f => p %>

       <% end %>

      <% else  %>

       <% parts_page =  f.object.page_parts_pages.build if   f.object.page_parts_pages.blank? %>

    <%= f.fields_for :page_parts_pages, parts_page do |p| %>

      <%= render 'page_part_page_form_field', :f => p %>

    <% end %>

  <% end %>

<% end %>

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.

It's working perfectly fine for new action but for edit action it is not displaying correctly.

Any help is highly appreciated.

Thanks in advance

vikram
  • 423
  • 4
  • 19

1 Answers1

0

You are creating new page_parts in this form:

parts =  f.object.page_parts.build if f.object.page_parts.blank?
parts_page =  f.object.page_parts_pages.build if   f.object.page_parts_pages.blank?

'build' creates new objects (it will not persist them in database though). So, no wonder it works for new, but not for edit. You can try this:

  <% page_fragment.each do |k,v| %>
     <%  if v.blank? %>
       <%= f.fields_for :page_parts do |p| %>
         <%= render 'page_part_form_field', :f => p %>
       <% end %>
      <% else  %>
    <%= f.fields_for :page_parts_pages do |p| %>
      <%= render 'page_part_page_form_field', :f => p %>
    <% end %>
  <% end %>
<% end %>

rails api has pretty good documentation of forms_for and other form helpers.

Salil
  • 9,534
  • 9
  • 42
  • 56
  • Thanks @salil for your time, but using your solution raised two problems:- 1) The form posted the only one instance of page_parts_pages whereas it should be two. 2) It raised an error Can't mass-assign protected attributes: page_parts_page, page_part. After overcoming this error, application raised "unknown attribute page_parts_page" – vikram Jun 29 '12 at 05:46
  • Edited my answer. I think if you know what fields_for does, you will find the correct way to implement your form. – Salil Jun 29 '12 at 16:36
  • I will try the edited answer. And also, it will be great for me if you share some links where i can understand the fields_for better – vikram Jun 30 '12 at 18:40