如何在 Rails 应用程序中使用 form_for 发布销毁操作? [英] How to post to destroy action with form_for in Rails app?

查看:55
本文介绍了如何在 Rails 应用程序中使用 form_for 发布销毁操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Rails 应用中,我希望我的 用户 在销毁他们自己的帐户之前输入他们的密码.

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

所以在我的 routes.rb 中,我将它添加到了我的 user 资源中:

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

resources :users do
  member do
    get :terminate
  end
end

在我的 terminate.html.erb 中,我有一个简单的表单:

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

在我的 users_controller.rb 中,我有这个:

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"

我在这里遗漏了什么?

感谢您的帮助.

推荐答案

这应该有效:

<%= form_for @user, method: :delete do |f| %>

这篇关于如何在 Rails 应用程序中使用 form_for 发布销毁操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆