Cocoon Gem 非绑定形式的嵌套属性
Coccon Gem Nested Attributes for non binding form
我有模特Activity
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
控制器购买动作 Activity
class PurchaseController < ApplicationController
def activity
@activity = Activity.find_by_id(params[:id])
@activity.activity_documents.build
end
end
再培训局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。创建 Activity 本身或从 PurchaseController activity。
我需要通过 activity 创建上传的文档不能通过 activity 的 PurchaseController 操作进行编辑/删除,因为将有两种不同类型的文档,并且将由两个上传不同类型的用户。
另一个选项可能是只允许上传新文档而不允许编辑以前的文档。
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>
我有模特Activity
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
控制器购买动作 Activity
class PurchaseController < ApplicationController
def activity
@activity = Activity.find_by_id(params[:id])
@activity.activity_documents.build
end
end
再培训局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。创建 Activity 本身或从 PurchaseController activity。
我需要通过 activity 创建上传的文档不能通过 activity 的 PurchaseController 操作进行编辑/删除,因为将有两种不同类型的文档,并且将由两个上传不同类型的用户。
另一个选项可能是只允许上传新文档而不允许编辑以前的文档。
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>