繁体   English   中英

Rails如何为销毁动作呈现两种不同的模态

[英]Rails how to render two different modals for destroy action

当银行经理必须删除没有客户bank_employee.users = nil bank_employee时,我想添加确认模式。 如果bank_employee有客户,我想呈现不同的模式destroy_confirmation_modal 如何以适当的方式做到这一点? if有条件,我应该放在哪里?

代码被剪辑edit.html.erb

  <div class="row">
    <div class="col-sm-12 col-md-12 col-xs-12 text-center bank-employee__button-wrapper bank-employees-users-registration__registrations-submit--wrapper">
      <a href="javascript:void(0)" id="primaryDestroyButton" class="bank-employee__button bank-employee__button-delete"><%= t('.delete') %></a>
      <%= f.submit t('.submit'), id: "formSubmit", class: "bank-employee__button bank-employee__button-submit"%>
    </div>
  </div>
<% end %> // this `end` comes from `form_for`
<%= button_to "", bank_employee_path(@bank_employee), method: :delete, form: {id: "bankEmployeeDestroyForm" }, class: "bank-employee__button-destroy" %>
<div class="row">
  <div class="col-xs-12 col-sm-12 col-md-12 text-center">
    <%= link_to bank_employees_path do %>
      <span class="bank-employee__back-button">
        <%= image_tag "icon_back.svg", alt: "Back icon", class: ""%>
        <%= t('.back') %>
      </span>
    <% end %>
  </div>
</div>
</div>
<%= render "destroy_confirmation_modal" %>

我不认为应该更新控制器方法,但也许我错了?

controller.rb

def destroy
  authorize current_bank_employee
  @bank_employee = find_bank_employee(params[:id])
  if @bank_employee.users.any? && associated_bank_employees.present?
    reassign_users_and_notify_bank_employee
  elsif @bank_employee.users.any?
    render :edit
  else
    @bank_employee.destroy
    render :destroy_notice, locals: { old_bank_employee: @bank_employee, assigned: false }
  end
end

编辑

我的routes.rb

resources :bank_employees, except: [:show], concerns: [:with_datatable] do
  member do
    get :confirm
  end
end

rails routes向我显示此路径为confirm_bank_employee因此if条件如下,我已进行了更改

<% if @bank_employee.users.empty? %>
  <%= button_to "", confirm_bank_employee_path(@bank_employee), form: {id: "bankEmployeeDestroyForm" }, class: "bank-employee__button-destroy", remote: true %>
<% else %>
  <%= button_to "", bank_employee_path(@bank_employee), method: :delete, form: {id: "bankEmployeeDestroyForm" }, class: "bank-employee__button-destroy" %>
<% end %>

您需要一种不同的方法。

<% if @bank_employee.clients.empty? %>
 <%= button_to "", bank_employee_confirm_path(@bank_employee), form: {id: "bankEmployeeDestroyForm" }, class: "bank-employee__button-destroy", remote: true %>


<% else %>

 <%= button_to "", bank_employee_path(@bank_employee), method: :delete, form: {id: "bankEmployeeDestroyForm" }, class: "bank-employee__button-destroy" %>
<% end %>

现在您需要两件事:

在routes.rb中,为您的blank_employee_confirm_path创建一个集合:

无论您使用哪种设置,我都假设您有某种blank_employees资源路径:

resources :blank_employees do
 member do
  get :confirm
 end
end

现在,在您的控制器内部,您需要添加方法确认:

def confirm
 ## do whatever you want do to here

 render :js
end

然后将转到confirm.js

在blank_employees视图文件夹中创建一个Confirm.js.erb文件。 为了确认这是可行的,您可以在其中添加console.log('it works')。

确认它可以正常工作后,您可以将JavaScript代码添加到Confirm.js.erb文件中:

$('#modal-body').html('<%= escape_javascript(render :partial => 'shared/your_confirmation_modal'%>');

使用此设置,您需要在edit.html.erb文件中使用<div id="modal-body"></div>作为模态。 还要注意,在我的示例中,确认模式存储在"views/shared/_your_confirmation_modal.html" 更改您的设置路径或创建确切的路径以使此工作!

请注意,“确认”路径适用于没有客户的blank_employees。 仅当没有客户端时才呈现按钮。 您之前为客户的blank_employees提供的所有其他逻辑保持不变。 您无需更改任何内容。 如果您内部有任何空白的逻辑,而没有任何客户端,请将代码移至Confirm方法。

还有一件事:确保还向您的destroy方法中添加一个render:js,然后在destroy.js.erb内部添加与Confirm.js.erb内部相同的逻辑,此外,您还希望为blank_employees呈现模式。与客户。 您的destroy.js.erb文件应如下所示:

 $('#modal-destroy').html('<%= escape_javascript(render :partial => 'shared/your_destroy_modal'%>');

非常重要:与第一个模态一样,将<div id="modal-destroy"></div>到您的edit.html.erb文件中,否则它将无法呈现模态。

如果不清楚,请通知我!

问候!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM