【问题标题】:Modal Pop up window on Rails Controller ActionRails 控制器操作上的模态弹出窗口
【发布时间】:2016-03-01 09:46:42
【问题描述】:

在我的客户索引视图中,如果我单击删除客户按钮,我的客户控制器删除方法中会发生以下操作。

Customers_controller.rb

类CustomersController

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

所以根据我的情况,如果它是真的意味着客户不能被删除,并且该消息将显示在客户索引页面本身的模态弹出窗口中,如何实现?

【问题讨论】:

    标签: ruby-on-rails bootstrap-modal


    【解决方案1】:

    我建议您在单击按钮时发出 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
    

    这应该可以正常工作。请记住,它肯定需要一些返工和调整:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-13
      • 2013-05-03
      • 1970-01-01
      • 2012-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多