如何使用Rails应用程序中的form_for发布以销毁操作?

时间:2013-10-21 10:49:54

标签: ruby-on-rails ruby ruby-on-rails-3

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

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

resources :users do
  member do
    get :terminate
  end
end

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

<%= 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 中,我有:

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

但是,当我点击提交时,我收到此错误:

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

我在这里缺少什么?

感谢您的帮助。

1 个答案:

答案 0 :(得分:10)

这应该有效:

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