Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to post to destroy action with form_for in Rails app?

In my Rails app I want my users to enter their password before destroying their own account.

So in my routes.rb I added this to my user resource:

resources :users do
  member do
    get :terminate
  end
end

In my terminate.html.erb I have this simple form:

<%= form_for(:user, :controller => "users", :action => "destroy", :html => {:method => "delete"}) do |f| %>

  <%= f.label :password %><br/>
  <%= f.password_field :password %>

  <%= f.submit "Terminate account" %><br/>

<% end %>

In my users_controller.rb I have this:

def terminate
  @user = User.find(params[:id])
  @title = "Terminate your account"
end

def destroy

  # make sure entered password is correct etc...

  @user.destroy
  flash[:success] = "Your account was terminated."
  redirect_to root_path
end

However, when I click submit I get this error:

No route matches [DELETE] "/en/users/7/terminate"

What am I missing here?

Thanks for any help.

like image 659
Tintin81 Avatar asked Oct 21 '13 10:10

Tintin81


1 Answers

This should work:

<%= form_for @user, method: :delete do |f| %>
like image 52
Marek Lipka Avatar answered Sep 28 '22 02:09

Marek Lipka