当用户点击按钮销毁两个产品之间的关系时,我试图通过ajax呈现部分内容。销毁动作有效,但我无法将部分渲染工作。
部分在产品的编辑页面上呈现:
edit.html.erb
<div id="relationship_form">
<%= render "relationships", product: @product %>
</div>
_relationships.html.erb
<%= render "unrelate", relationship: relationship %>
_unrelate.html.erb
<%= form_for(relationship,html: { method: :delete },remote: true) do |f| %>
<%= f.submit "Remove", class: "btn btn-small" %>
<% end %>
relationships_controller.html.erb
def destroy
...
respond_to do |format|
format.html { redirect_to edit_product_path }
format.js
end
end
destroy.js.erb
$("#relationship_form").html("<%= escape_javascript(render :partial => 'products/relationships', :locals => {:product => @product}) %>");
我已经检查过ajax调用是否通过插入警告进入destroy.js.erb,所以javascript中一定有错误?
更新:
我通过将_unrelate partial中的表单移动到_relationship partial来实现它。仍然不太清楚它为什么会起作用。
_relationships.html.erb
<%= form_for(relationship,html: { method: :delete },remote: true) do |f| %>
<%= f.submit "Remove", class: "btn btn-small" %>
<% end %>