【问题标题】:How to post to destroy action with form_for in Rails app?如何在 Rails 应用程序中使用 form_for 发布销毁操作?
【发布时间】:2013-10-29 19:48:17
【问题描述】:

在我的 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"

我在这里错过了什么?

感谢您的帮助。

【问题讨论】:

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


    【解决方案1】:

    这应该可行:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 2017-09-07
      • 1970-01-01
      相关资源
      最近更新 更多