簡體   English   中英

如何識別rails form_tag中的Id屬性?

[英]How to recognize Id attribute in rails form_tag?

我在rails中使用表單來傳遞一個id。 該 id 將觸發 javascript Sweet Alert 功能,該功能將彈出一個帶有確認消息的模式。 當消息被確認“是”時,用戶將刪除他們的帳戶,這意味着將被重定向到 rails 路線。 這就是應該發生的事情。 我無法讓 id 在表單中觸發並調出模式,但路由運行良好。 讓我向您展示我嘗試實現此目的的兩種方法。

Ruby on Rails

<%= form_tag(registration_path(current_user), {method: :delete}) do %>
 <%= hidden_field_tag('deleteBtn') %>
 <button type="submit"><i class="fa fa-trash"></i>Delete Account</button>
<% end %>

使用甜蜜警報的 Javascript

$('#deleteBtn').on('click', function(e) {
          e.preventDefault();
          swal({
              title: 'Decline Inquiry',
              text: 'Are you sure you want to delete your account? This will delete all records of your entire account.',
              icon: 'warning',
              buttons: [ 'Yes', 'No']
          }).then(isNo => {
              if(isNo) return;
              $('#deleteBtn').submit();
          });
      });

請理解我已經嘗試了 form_tag 的幾種變體,當我檢查它時,html id 甚至不會出現:

<%= form_tag 'registration_path(current_user)', ({method: :delete, id: "deleteBtn"}) do %>
 <button type="submit"><i class="fa fa-trash"></i>Delete Account</button>
<% end %>

現在需要注意的是,我可以刪除表單並僅使用傳遞 id 的按鈕和模式工作,但是在 Javascript 中訪問 rails 路由失敗並且我收到路由錯誤消息。 我剛換了

$('#deleteBtn').submit(); 

$.get('<%= link_to registration_path(current_user), method: :delete, class: 'delete' do %><% end %>');

或者

window.location.pathname('<%= link_to registration_path(current_user), method: :delete, class: 'delete' do %><% end %>');

我已經為此工作了一整天,感謝您提供任何幫助。 由於我無法在 Javascript 中訪問 rails 路線,我唯一的選擇是使用表單。

版本:Rails 4.2.4

您可能會誤解link_to的用法。 link_to不僅會創建一個路徑,還會創建一個錨 HTML 元素,就像 rails doc ( https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to ) 示例顯示:

link_to "Remove Profile", profile_path(@profile), method: :delete
# => <a href="/profiles/1" rel="nofollow" data-method="delete">Remove Profile</a>

如果您想刪除一個賬戶,您需要發送一個帶有“DELETE”操作的請求(假設您使用resourcesconfig/routes.rb中生成與您的賬戶相關的路由)。 要發送帶有“DELETE”操作的請求,在 Rails 中有幾種方法:

  1. 使用:delete方法創建一個表單並通過表單提交觸發它,就像您的第一個表單一樣。
  2. 使用button_to:delete方法,它仍然使用帶有提交按鈕的表單,但是表單是由button_to生成的。
  3. 使用帶有:delete方法的link_to ,它會創建一個錨點來使用 Rails UJS 執行“DELETE”請求,但它在 Rails 7 中已被棄用。

如果不希望表單發送“DELETE”請求,仍然可以使用 JS 發送。 (不使用$.getwindow.location ,它們用於“GET”請求。)但這不是一項簡單的工作,因為您需要處理 Rails 表單自動為您執行的 CSRF 保護。

在我看來,使用表單提交來發送“DELETE”請求仍然是在 Rails 中做的最好的方式,就像你以前做過的那樣。 如果您不想在頁面上顯示表單,也許使用 css 隱藏它?

更新

如果您使用設計默認路由,刪除帳戶時不需要 id。 Devise只提供當前用戶的賬號刪除功能,即只有登錄用戶自己才能刪除自己的賬號。

如果要刪除其他用戶,則需要創建自己的控制器來執行此操作。 您可以參考其他相關的 SO: https ://stackoverflow.com/a/42144398/232710

更新

給表單添加id屬性非常簡單,只需在form_tag中添加id選項,如下所示:

<%= form_tag(registration_path, { method: :delete, id: "account_delete_form" }) do %>
 <button type="submit"><i class="fa fa-trash"></i>Delete Account</button>
<% end %>

生成的表單 HTML 就像

<form id="account_delete_form" action="/users" accept-charset="UTF-8" method="post">
  <input type="hidden" name="_method" value="delete" autocomplete="off">
  <input type="hidden" name="authenticity_token" value="iItLCVuN9N59HolXe7OQ-erHcO-Iz78vI0ghqdliU55CjuI1uyemcMlwW-WxeS8iXqCT1-ezaNlTqHcUJ9ynSA" autocomplete="off">
  <button type="submit">
    <i class="fa fa-trash"></i> Deletet Account
  </button>
</form>

然后就可以用JS觸發表單提交了:

$("#account_delete_form").submit();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM