0

我正在创建一个表单供管理员进入并列出、编辑和删除用户。我尝试了许多不同的变体来删除用户,但没有任何效果。我想知道是不是因为我需要在我的routes.rb中使用devise_for :usersand 。resources :users这是因为我有链接到用户的上传/附件。但这是我的链接

<%= link_to 'Delete',user, :method => 'delete',  :confirm => 'Are you sure?' %>

还有我的 routes.rb

# devise_for :users
devise_for :users do
  get "/users/sign_out" => "devise/sessions#destroy", :as => :destroy_user_session
end
resources :users  do
  resources :attachments
end

我收到的错误是The action 'destroy' could not be found for UsersController.

但是我的用户控制器有

def destroy
  @user = User.find(params[:id]).destroy
  redirect_to admin_index, :flash => { :success => 'User was successfully deleted.' }
end
4

1 回答 1

1

如果您没有对请求使用 ajax,那么问题在于您使用的是link_to

为了发送 :method => 'delete' 你必须使用一个按钮,就像这样:

<%= button_to 'Delete', user, :method => 'delete',  :confirm => 'Are you sure?' %>

因为必须通过表单提交来执行破坏性操作:

http://www.w3.org/2001/tag/doc/whenToUseGet.html#checklist

于 2012-10-26T16:34:35.210 回答