bootstrap-确认不处理方法:rails 应用程序中的删除

bootstrap-confiirmation doesn't handle method :delete in rails app

我正在尝试将 bootstrap-confirmation 用于包含数据表的索引视图和用于删除行项目的列。我让它在没有确认的情况下工作,或者在默认确认下工作,但没有 bootstrap-确认,它将我发送到 show 方法,而不是控制器的 delete 方法。它就像它没有看到方法::delete

这是我在索引视图中调用它的方式。弹出确认显示,但当我单击“确定”时,它会将我转到显示页面。

<td><%= link_to '<i class="fa fa-trash-o fa-lg"></i>'.html_safe, role_path(id: role.id), method: :delete, :'data-toggle' => 'confirmation', :'data-copy-Attributes' => 'href data-method'%></td>

以下是有效的(未经确认),所以我知道我的路线、控制器操作等有效。

<td><%= link_to '<i class="fa fa-trash-o fa-lg"></i>'.html_safe, role_path(id: role.id), method: :delete, %></td>

有什么想法吗?

这是我的观点:

<h1> Roles</h1>
</br>

<table width="100%" class="table table-striped table-bordered table-hover" id="roles-table">
  <thead>
    <tr>
      <th>Roles</th>
      <th>User Count</th>
      <th>Delete Role</th>
    </tr>
  </thead>
  <tbody>
    <% @roles.each do |role| %>
    <tr>
      <td><%= role.name %>
      <td><%= role.users.count %></td>
      <% if (role.users.count == 0) %>
        <!--td><%= link_to '<i class="fa fa-trash-o fa-lg"></i>'.html_safe, role_path(id: role.id), method: :delete, :data => {:confirm => 'Are you sure?'}%></td-->
        <td><%= link_to '<i class="fa fa-trash-o fa-lg"></i>'.html_safe, role_path(id: role.id), method: :delete, :'data-toggle' => 'confirmation', :'data-copy-Attributes' => 'href data-method'%></td>

      <% else %><
        <td></td>
      <% end %>
    </tr>
    <% end %>
  </tbody>
</table>
<br>
<%= link_to "Add Role", new_role_path, class: "btn btn-success"%>

我无法让库工作,因为它没有提供编辑它生成的弹出窗口 html 的方法,所以,它也为我带来了 link 到 show 方法,无法删除它。

所以我所做的是创建一个路由来删除期望从资源中接收 id 的资源以及 delete:

的参数值
get 'role/:id/:delete', to: 'role#show', as: 'show_role'

然后在你的控制器中你可以检查你是否接收到一个delete参数,以便在showdestroy之间分开,如果你接收到它,那么你销毁它并重定向到 roles_url:

def show
  @role = Role.find(params[:id])
  if params[:delete]
    @role.destroy
    redirect_to roles_url
  end
end

然后在您的视图中,您可以使用 link_to 帮助程序,传递之前创建的路由传递资源和 delete 参数的值,添加数据属性 Bootstrap 确认需要让它工作:

<%= link_to 'Destroy', show_role_path(role, delete: true), data: { toggle: 'confirmation', title: 'Delete it?' } %>