【发布时间】:2016-09-30 16:53:58
【问题描述】:
我目前有一个删除对象的简单 form_for。现在它是一个静态页面,每次删除对象时都会刷新。我们收到一个客户请求,希望索引页面在不刷新页面的情况下删除对象。显然 ajax 可以解决这个问题,但我对 javascript 的经验非常有限。如果有人能指出我正确的方向,我将不胜感激。为了清楚起见,这是我的代码。
表格
这是按下删除时弹出模式的表单。
<%= link_to 'Delete', delete_snitch_path(snitch), rel: 'modal:open',
data: { tooltip: "Delete" },
class: 'icon icon-delete delete', title: "Delete #{snitch.name}" %>
-
<%= form_for @snitch, html: { class: "form-actions", method: 'delete' } do |form| %>
<span class="button-text"><%= link_to 'NO WAY!', home_base_url_or_default(root_path), rel: "modal:close" %></span>
<input type="submit" class="button button--modal" value="Yes, delete it.">
<% end %>
控制器
def destroy
DestroySnitch.perform(snitch: @snitch)
respond_to do |format|
format.html do
redirect_to snitches_path, notice: "Snitch was successfully deleted."
end
format.json do
render json: @snitch.all
end
end
end
查看
<table class="snitch-list">
<%= render partial: 'snitch', collection: @presenter.snitches %>
</table>
当前的 JS(不工作 OBVI)
$('ajax').bind('ajax:success', function() {
$(this).closest('tr').fadeOut();
});
整个告密部分
<tbody>
<tr class="<%= snitch.classes %>">
<td>
<%= link_to "<span class='icon led'></span><span>#{snitch.name}</span>".html_safe, snitch_path(snitch), class: "name"%>
</td>
<td class="interval"><span class="vspace"><%= snitch.interval %></span></td>
<td class="last-checked">
<span class="vspace">
<% if snitch.source.checked_in_healthy_at %>
<span data-tooltip="Checked in healthy at UTC(<%= snitch.source.checked_in_healthy_at.to_i %>) || LOCAL(<%= snitch.source.checked_in_healthy_at.to_i %>)">
Last seen <strong><%= snitch.checked_in_healthy_at(title: nil) %></strong>
</span>
<% else %>
<strong><%= snitch.checked_in_healthy_at %></strong>
<% end %>
</span>
</td>
<td class="snitch-controls" data-icons="<%= snitch.pauseable? ? "5" : "4" %>">
<%= render 'menu', snitch: snitch %>
<nav class="snitch-states" >
<% if snitch.pauseable? %>
<%= link_to 'Pause', pause_snitch_path(snitch), class: 'icon icon-pause pause',
data: { tooltip: "Pause" },
rel: "modal:open" %>
<% end %>
<%= link_to 'Delete', delete_snitch_path(snitch), rel: 'modal:open',
data: { tooltip: "Delete" },
class: 'icon icon-delete delete', title: "Delete #{snitch.name}" %>
</nav>
</td>
</tr>
</tbody>
我没有任何 javascript,我刚刚开始这个故事,但我在谷歌搜索时已经感到困惑。另外,有没有用 Rspec 和 Capybara 测试 ajax 的好方法?
【问题讨论】:
-
为了回答这个问题,我需要弹出要求确认的模态代码。 ajax 将绑定到该按钮,而不是表单中的按钮。你可以发布模式代码吗?拥有该项目所在页面的代码也会很有帮助,这样隐藏或删除 div 的代码也可以在答案中。
-
@RockwellRice 已更新。如果您需要更多信息,请告诉我
-
我没有看到模态的 html,看起来表单中的按钮打开模态正确吗?当按下模态内的按钮时,将发生实际的删除,这是我编写代码时需要看到的按钮。另外,给出那个部分里面的代码,我需要查看将被删除或隐藏的实际 div。
标签: javascript ruby-on-rails ajax rspec