Rails,fields_for嵌套属性 - 避免在编辑时显示现有对象

时间:2016-08-09 15:25:19

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 rails-activerecord

我想让我的编辑表单只允许创建新元素。目前,它会自动显示所有现有元素。以下是我目前的代码。我在订单和文档之间有一个标准的has_many,belongs_to关系。我只希望用户能够添加新文档,从不编辑现有文档。使用当前代码它会在编辑表单中显示所有相关文档。

    <%= 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

使用下面的代码,我可以正确显示表单(不显示现有文档),但是当通过编辑表单添加新文档时,文档实际上从未创建过。

<% 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 %>

任何帮助/建议/资源将不胜感激。被困在这一点上。此外,我还没有重构为部分,所以请原谅杂乱。 谢谢!! -Vinny

0 个答案:

没有答案