我已经安装了 cocoon 并根据此处概述的示例添加了所有必要的更改:https ://github.com/nathanvda/cocoon/wiki/ERB-examples
然而由于某种原因,我无法在我的局部中看到任何东西。
视图/order_items/_order_item_fields.html.erb:
<div class="nested-fields">
<div class="field">
<%= f.label :material %>
TEST
</div>
<%= link_to_remove_association "remove order_item", f %>
</div>
意见/订单/new.html.erb:
<div class="container text-center mt-4">
<%= form_for @order do |f| %>
<div class="container">
<div class="row">
<h2>New Order</h2>
</div>
</div>
<div id="order_items">
<%= f.fields_for :order_items do |order_item| %>
<%= render "order_items/order_item_fields", f: order_item %>
<% end %>
<div class="links">
<%= link_to_add_association "add order_item", f, :order_items %>
</div>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
</div>
订单.rb:
class Order < ApplicationRecord
belongs_to :user, optional: true
has_many :order_items
accepts_nested_attributes_for :order_items, reject_if: :all_blank, allow_destroy: true
end
我也收到缺少模板错误:
Missing partial orders/_order_item_fields, application/_order_item_fields with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :coffee, :jbuilder]}.
我相信如果我试图从另一个视图文件夹呈现部分,那么在部分名称之前添加文件夹应该可以工作:
<%= render "order_items/order_item_fields", f: order_item %>
如果我_order_item_fields.html.erb
从views/order_items
我的views/orders
目录移动,页面将呈现,但我仍然看不到order_item_fields
部分内部的任何内容。