非绑定形式的Coccon宝石嵌套属性

时间:2018-10-12 15:18:45

标签: ruby-on-rails cocoon-gem

我有模特活动

class Activity < ApplicationRecord
  accepts_nested_attributes_for :activity_documents, reject_if: proc { |attributes| attributes[:document].blank? && attributes[:document_title].blank? }, allow_destroy: true
end

另一个模型

class ActivityDocument < ApplicationRecord
  mount_uploader :document, DocumentUploader
  belongs_to :activity
end

具有动作活动的管制员购买

class PurchaseController < ApplicationController
  def activity
    @activity     = Activity.find_by_id(params[:id])
    @activity.activity_documents.build
  end
end

ERB activity.html.erb

  <%= simple_form_for @activity, :url => take_activity_path, :method => :post , html: { class: "exercise-form"} do |f| %>

  <!-- nested attributes start -->
  <section class="show-section">
    <div class="row">
      <div class="col l12"><h4>Documents</h4></div>
      <div class="col s12">
       <%= f.simple_fields_for :activity_documents do |builder| %>
        <% render 'activity_document_fields', f: builder %>
       <% end #f.simple_fields_for%>
         <div class="links">
          <%= link_to_add_association 'Add Documents', f, :activity_documents, class: "btn mb-2" %>
        </div>
      </div>
    </div>
  </section>

<% end %>

_activity_document_fields.html.erb

<div class="nested-fields">
    <div class="row">
        <div class="col l12">
            <%= f.input :document_title %>
            <%= f.hidden_field :user_id, :value => current_user.id  %>  
            <%= f.hidden_field :user_type, :value => current_user.role  %>
        </div>
        <div class="col l8">
            <%= f.input :document %>
        </div>
        <div class="col l1 mt-3">
            <%= link_to_remove_association "delete", f, class: "material-icons teal-text text-lighten-1" %>
        </div>
    </div>
</div>

现在可以从两个不同的位置上载与此文档相关的activity_documents。创建活动本身或从PurchaseController活动中创建。

我需要通过活动创建上载的文档不可通过活动的PurchaseController操作进行编辑/删除,因为将有两种不同类型的文档,并且将由两种不同类型的用户上载。

另一种选择是仅允许上传新文档,而不允许编辑先前的文档。

1 个答案:

答案 0 :(得分:0)

This的答案似乎可以解决我的问题。

我们进行了以下更改:

<section class="show-section">
  <div class="row">
    <div class="col l12"><h4>Documents</h4></div>
    <div class="col s12">
      <%= f.simple_fields_for :activity_documents do |builder| %>
        <% render 'activity_document_fields', f: builder %>
      <% end #f.simple_fields_for%>
      <div class="links">
       <%= link_to_add_association 'Add Documents', f, :activity_documents, class: "btn mb-2" %>
      </div>
    </div>
  </div>
</section>

  <section class="show-section">
    <div class="row">
      <div class="col l12"><h4>Documents</h4></div>
      <div class="col s12">
       <%= f.simple_fields_for :activity_documents, @activity.activity_documents.build do |builder| %>
        <% render 'activity_document_fields', f: builder %>
       <% end #f.simple_fields_for%>
         <div class="links">
          <%= link_to_add_association 'Add Documents', f, :activity_documents, class: "btn mb-2" %>
        </div>
      </div>
    </div>
  </section>