简体   繁体   中英

How to recognize Id attribute in rails form_tag?

I am using a form in rails to pass an id. That id will trigger the javascript Sweet Alert function which will pop up a modal with a confirmation message. When the message is confirmed "Yes" user will have their account deleted meaning will be redirected to the rails route. 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. Let me show you two ways I tried to accomplish this.

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

$('#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 '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. 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.

Version: Rails 4.2.4

You may misunderstand the link_to usage. 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 "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 ). To send a request with "DELETE" action, there are several ways to do in rails:

  1. Create a form with :delete method and trigger it with a form submit, just like your first form.
  2. Use button_to with :delete method, it still uses a form with a submit button, but the form is generated by 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.

If you don't want a form to send the "DELETE" request, you still can use JS to send it. (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.

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. If you don't want to show the form on the page, maybe use css to hide it?

UPDATE

No id is required when deleting an account if you use the Devise default routes. Devise only provides the account deletion for current user, ie, only logged-in user self can delete his/her own account.

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

UPDATE

To add id attribute to form is quite easy, just add id option in form_tag as below:

<%= 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

<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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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