您好我正在为我的集合模型运行一个循环,如下所示:
产品/ show.html.erb
<div class="card card-block">
<%= render partial: 'products/collects', locals: {product: @product} %>
</div>
部分_collects.html.erb
<% current_user.collections.each do |collection| %>
<div class="row">
<div class="col-lg-6 d-flex justify-content-start">
<h4><%= collection.name %></h4>
</div>
<div id="collection_<%= collection.id %>_<%= product.id %>_collects">
<% if collection.collects?(product) %>
<%= link_to product_collect_path(product.id, collection_id: collection.id), method: :delete, remote: true, id: 'remove-button' do %>
<i class="fa fa-times" data-toggle='tooltip' data-placement='top' title='Remove this Product'></i>
<% end %>
<%- else -%>
<%= link_to product_collect_path(product.id, collection_id: collection.id), method: :post, remote: true, id: 'collects-button' do %>
<i class="fa fa-plus" data-toggle='tooltip' data-placement='top' title='Collect this Product'></i>
<% end %>
<% end %>
</div>
</div>
<% end %>
<br>
<div class="row">
<%= link_to 'Create new Collection', new_collection_path, class: 'btn btn-secondary btn-block' %>
</div>
这很好用,除非我想用js:
来定位下面的div<div id="collection_<%= collection.id %>_<%= product.id %>_collects">
有没有办法运行循环,只在部分
中包含以下内容<div id="collection_<%= collection.id %>_<%= product.id %>_collects">
<% if collection.collects?(product) %>
<%= link_to product_collect_path(product.id, collection_id: collection.id), method: :delete, remote: true, id: 'remove-button' do %>
<i class="fa fa-times" data-toggle='tooltip' data-placement='top' title='Remove this Product'></i>
<% end %>
<%- else -%>
<%= link_to product_collect_path(product.id, collection_id: collection.id), method: :post, remote: true, id: 'collects-button' do %>
<i class="fa fa-plus" data-toggle='tooltip' data-placement='top' title='Collect this Product'></i>
<% end %>
<% end %>
</div>
或者更新下面的js并定位div更好,而不是部分?
产品/收集/ create.js.erb
$('#collection_<%= @collection.id %>_<%= @product.id %>_collects').html("<%=j render partial: 'products/collects', locals: {product: @product} %>");
更新
我发现了一些其他问题,这些问题导致我失去部分和渲染循环的rabit漏洞。具体为this和the docs
为集合添加第二个本地和一个对象,如此
<%= render partial: 'products/collects', locals: {product: @product, collection: collection}, object: collection %>
我能够在没有方法错误的情况下显示页面,但它显示了 like so
我做错了什么?
答案 0 :(得分:0)
我发现通过在集合循环内部渲染部分
<% @collections.each do |collection| %>
<%= collection.name %><
<%= render partial: 'products/collects', locals: {product: @product, collection: collection} %>
<% end %>
并像我这样设置我的部分:
<div id="collection_<%= collection.id %>_<%= product.id %>_collects">
<% if collection.collects?(product) %>
<%= link_to product_collect_path(product.id, collection_id: collection.id), method: :delete, remote: true, id: 'remove-button' do %>
<i class="material-icons">subtract</i></i>
<% end %>
<%- else -%>
<%= link_to product_collect_path(product.id, collection_id: collection.id), method: :post, remote: true, id: 'collects-button' do %>
<i class="material-icons">add</i>
<% end %>
<% end %>
</div>
我能够运行循环并渲染部分。在我最初的本地人电话中,我错过了收藏:收藏。我建议沿着部分和渲染循环的兔洞走下去,如我的更新中所述。