mardi 9 août 2016

Rails, fields_for nested attributes - Avoid displaying existing objects on edit

I am trying to have my Edit form only allow for the creation of new elements. Currently it automatically displays all of the existing elements. Below is my current code. I have a standard has_many, belongs_to relationship between Orders and Documents. I only want the ability for users to add new documents and never edit the existing documents. With the current code It displays all associated Documents on the edit form.

    <%= f.fields_for :documents, child_index: Time.now.to_i do |document| %>
  <div class="col-md-5">
    <%= document.label :doc_type, "Type", :style => "color:red;" %>
    <%= document.collection_select :doc_type_id, DocType.order(:name), :id, :name, { prompt: true, class: "form-full"} %>
  <br>
  </div>
  <div class="col-md-7">
    <%= document.label :description,"Description **", :style => "color:red;" %>
    <%= document.text_field :description, style:"color:black;" %>
    <br>
  </div>
  <div class="col-md-12">
  <br>
    <%= document.file_field :doc, style:"color:white;" %>
  </div>
  <div class="col-md-12" style="background:#272727;">
    <hr class="style2">
  </div> 
<% end %>


class Order < ActiveRecord::Base
  belongs_to :user
  has_many :properties, :dependent => :destroy 
  has_many :documents, :dependent => :destroy


  accepts_nested_attributes_for :documents,
  :allow_destroy => true,
  :reject_if     => :all_blank
  accepts_nested_attributes_for :properties,
  reject_if: proc { |att| att["city"].blank? }

end

class Document < ActiveRecord::Base
  belongs_to :order
  belongs_to :doc_type
end

With The below code I was able to have the form appear correctly (not showing existing Documents) but when adding a new Document via the Edit form, the Document is never actually created.

<% if !current_page?(action: 'edit') %>
  <%= f.fields_for :documents, child_index: Time.now.to_i do |document| %>
    <div class="col-md-5">
      <%= document.label :doc_type, "Type", :style => "color:red;" %>
      <%= document.collection_select :doc_type_id, DocType.order(:name), :id, :name, { prompt: true, class: "form-full"} %>
    <br>
    </div>
    <div class="col-md-7">
      <%= document.label :description,"Description **", :style => "color:red;" %>
      <%= document.text_field :description, style:"color:black;" %>
      <br>
    </div>
    <div class="col-md-12">
    <br>
      <%= document.file_field :doc, style:"color:white;" %>
    </div>
  <div class="col-md-12" style="background:#272727;">
    <hr class="style2">
  </div> 
  <% end %>
<% else %>
  <%= f.fields_for @order.documents.build, child_index: Time.now.to_i do |document| %>
    <div class="col-md-5">
      <%= document.label :doc_type, "Type", :style => "color:red;" %>
      <%= document.collection_select :doc_type_id, DocType.order(:name), :id, :name, { prompt: true, class: "form-full"} %>
    <br>
    </div>
    <div class="col-md-7">
      <%= document.label :description,"Description **", :style => "color:red;" %>
      <%= document.text_field :description, style:"color:black;" %>
      <br>
    </div>
    <div class="col-md-12">
    <br>
      <%= document.file_field :doc, style:"color:white;" %>
    </div>
  <div class="col-md-12" style="background:#272727;">
    <hr class="style2">
  </div> 
  <% end %>
<% end %>

Any help/advice/resources would be appreciated. Been stuck on this for a bit. Also I have not refactored into partials yet so please forgive the messiness. THANKS!! -Vinny

Aucun commentaire:

Enregistrer un commentaire