I have a rails application I'm working on, and I'm using devise for authentication. I'm having an issue where when I try to update the user details instead up updating the user the user just gets deleted.
In registrations/edit.html.erb I have:
<%= form_for(resource, as: resource_name, url: registration_path(resource_name), method: :put) do |f| %>
... input tags and styling
<% end %>
Which generates the html:
<form class="edit_user" id="edit_user" enctype="multipart/form-data" action="/users" accept-charset="UTF-8" method="post">
<input type="hidden" name="_method" value="put" />
<input type="hidden" name="authenticity_token" value="Q2U4gEBfaIwCo2Vwi2fwXgwYLjzE4HpoDC8KN52m2GGqEdb94jd/c3TyFhEJtQEziHat9zFQs+e+fRJp4/j2WA==" />
... rest of the input tags
</form>
The key thing here being <input type="hidden" name="_method" value="put" />
in rails this should trigger the PUT route but for some reason when I submit the form it's triggering the delete route:
Started DELETE "/users" for 127.0.0.1 at 2020-06-01 16:15:48 +0100
(0.6ms) SET NAMES utf8mb4 COLLATE utf8mb4_bin, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
(0.2ms) SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
Has anyone come across this before and know what could be causing it?
EDIT
my config/routes.rb
# frozen_string_literal: true
Rails.application.routes.draw do
resources :posts
namespace :admin do
resources :users
root to: "users#index"
end
devise_for :users, module: 'users'
get 'profile', to: 'profile#index'
root to: "home#index"
end
Turns out I was just being silly. amongst the input fields there was a ERB button_to helper for canceling the account (or rather deleting the user) this helper was rendering a form inside the update form and that inner form was what was being submitted when hitting the submit button on the outer form, causing the delete action to be triggered
EDIT: To be clear the way to avoid this is to make sure the button_to helper is outside the form you're wanting to submit or on a separate page.