2016-03-01 41 views
0

在我的客户索引视图中,如果我单击删除客户按钮,则会在我的客户控制器删除方法中执行以下操作。Rails控制器上的Modal弹出窗口操作

Customers_controller.rb

类CustomersController < ApplicationController的 。 。

def destroy 
    if Customerarticle.where(author_id: params[:id]).count > 0 
     #####I need the logic here to display a modal window on my index page of customers here##### 
    else 
     Customer.find(params[:id]).destroy 
     flash[:success] = "Customer deleted" 
     # debugger 
     redirect_to customers_path 
    end 

所以根据我的情况,如果这是真的意味着用户无法删除,并显示在一个模式弹出窗口的客户索引页本身上的消息,该如何实现呢?

回答

0

我建议你发出一个ajax请求,当按钮被点击并使其重定向到你的销毁行动。然后,在控制器内执行你的逻辑,并简单地返回一个真/假(或其他任何你需要的)并在你的JS回调中使用它。

例如:

在你view.js

$(document).ready(function(){ 
    $("#delete_button").click(function(e){ 
     e.preventDefault(); 
    $.ajax({ 
     type: "POST", 
     url: "/URL/TO/CONTROLLER", 
     data: { id: someID}, 
     success: function(result){ 
     if(result == true) { 
      // Do something 
     } else { 
      window.location.replace(result); 
     } 
    }}); 
    }); 
}); 

在你的控制器:

def destroy 
    if Customerarticle.where(author_id: params[:id]).count > 0 
     return true 
    else 
     Customer.find(params[:id]).destroy 
     flash[:success] = "Customer deleted" 
     return customers_path 
    end 
end 

这应该只是罚款。请记住,它肯定需要一些重新调整和调整:)