简体   繁体   English

如何识别rails form_tag中的Id属性?

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

I am using a form in rails to pass an id.我在rails中使用表单来传递一个id。 That id will trigger the javascript Sweet Alert function which will pop up a modal with a confirmation message.该 id 将触发 javascript Sweet Alert 功能,该功能将弹出一个带有确认消息的模式。 When the message is confirmed "Yes" user will have their account deleted meaning will be redirected to the rails route.当消息被确认“是”时,用户将删除他们的帐户,这意味着将被重定向到 rails 路线。 That is what's supposed to happen.这就是应该发生的事情。 I cannot get the id to trigger in the form and bring up the modal, but the routing is working perfect.我无法让 id 在表单中触发并调出模式,但路由运行良好。 Let me show you two ways I tried to accomplish this.让我向您展示我尝试实现此目的的两种方法。

Ruby on Rails 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 using Sweet Alert使用甜蜜警报的 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();
          });
      });

Please understand I have tried several variations of form_tag and the html id won't even show up when I inspect it:请理解我已经尝试了 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 %>

Now something to note, I can drop the form and just use the button passing an id and the modal works, but accessing the rails route in Javascript fails and I get a routing error message.现在需要注意的是,我可以删除表单并仅使用传递 id 的按钮和模式工作,但是在 Javascript 中访问 rails 路由失败并且我收到路由错误消息。 I just replaced the我刚换了

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

with

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

or或者

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

I have been working on this a whole day now and any help is appreciated.我已经为此工作了一整天,感谢您提供任何帮助。 Since I can't access the rails routes in Javascript my only option is to use a form.由于我无法在 Javascript 中访问 rails 路线,我唯一的选择是使用表单。

Version: Rails 4.2.4版本:Rails 4.2.4

You may misunderstand the link_to usage.您可能会误解link_to的用法。 link_to would not only create a path but an anchor HTML element, just like the rails doc ( https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to ) example shows: 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>

If you want to delete an account, you need send a request with the "DELETE" action (Suppose you use the resources to generate your account related routes in config/routes.rb ).如果您想删除一个账户,您需要发送一个带有“DELETE”操作的请求(假设您使用resourcesconfig/routes.rb中生成与您的账户相关的路由)。 To send a request with "DELETE" action, there are several ways to do in rails:要发送带有“DELETE”操作的请求,在 Rails 中有几种方法:

  1. Create a form with :delete method and trigger it with a form submit, just like your first form.使用:delete方法创建一个表单并通过表单提交触发它,就像您的第一个表单一样。
  2. Use button_to with :delete method, it still uses a form with a submit button, but the form is generated by button_to .使用button_to:delete方法,它仍然使用带有提交按钮的表单,但是表单是由button_to生成的。
  3. Use a link_to with :delete method, it creates an anchor to perform a "DELETE" request by using Rails UJS, but it is already deprecated in Rails 7.使用带有:delete方法的link_to ,它会创建一个锚点来使用 Rails UJS 执行“DELETE”请求,但它在 Rails 7 中已被弃用。

If you don't want a form to send the "DELETE" request, you still can use JS to send it.如果不希望表单发送“DELETE”请求,仍然可以使用 JS 发送。 (Not use $.get nor window.location , they are for "GET" request.) But it's not a trivial work because you need handle the CSRF protection which the rails form does it automatically for you. (不使用$.getwindow.location ,它们用于“GET”请求。)但这不是一项简单的工作,因为您需要处理 Rails 表单自动为您执行的 CSRF 保护。

In my opinion, using a form submit to send the "DELETE" request is still the best way to do in rails, just like you have done before.在我看来,使用表单提交来发送“DELETE”请求仍然是在 Rails 中做的最好的方式,就像你以前做过的那样。 If you don't want to show the form on the page, maybe use css to hide it?如果您不想在页面上显示表单,也许使用 css 隐藏它?

UPDATE更新

No id is required when deleting an account if you use the Devise default routes.如果您使用设计默认路由,删除帐户时不需要 id。 Devise only provides the account deletion for current user, ie, only logged-in user self can delete his/her own account. Devise只提供当前用户的账号删除功能,即只有登录用户自己才能删除自己的账号。

If you want to delete other users, you need to create your own controller to do it.如果要删除其他用户,则需要创建自己的控制器来执行此操作。 You can references other related SO: https://stackoverflow.com/a/42144398/232710您可以参考其他相关的 SO: https ://stackoverflow.com/a/42144398/232710

UPDATE更新

To add id attribute to form is quite easy, just add id option in form_tag as below:给表单添加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 %>

The generated form HTML is like生成的表单 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>

Then you can trigger the form submit with JS:然后就可以用JS触发表单提交了:

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

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

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