我正在开发rails 3 app,我正在尝试link_to删除路径。这看起来非常简单,我的路线看起来像这样
namespace :admin do
resources :users, :except => :destroy do
...
delete :delete_contacts, :on => :collection
end
end
我的控制器看起来像这样
class Admin::UsersController < ApplicationController
...
def delete_contacts
user = User.find(params[:user_id])
user.contacts.destroy_all
redirect_to edit_admin_user_path(current_user.id)
flash[:notice] = "Successfully deleted #{user.name} contacts"
end
end
我当前的link_to看起来像这样
<%= link_to delete_contacts_admin_users_path(user_id: @user.id), method: :delete, data: { confirm: 'Are you sure you want to delete this users Contacts?' }, remote: true do %>
<h4 style="color: #BF3430;"><i class="material-icons">delete_forever</i> Delete Contacts</h4>
<% end %>
我已尝试使用和不使用:on => :collection
的路线,而且我似乎一直遇到同样的错误:
错误
Started DELETE "/admin/users/955/delete_properties/" for 127.0.0.1 at 2018-01-17 17:58:34 -0800
Processing by Admin::UsersController#delete_properties as JS
Parameters: {"id"=>"955"}
但是,所建立的链接/admin/users/delete_properties/?user_id=955
似乎只是需要的链接?
有谁看到我在这里做错了什么?这是非常简单的,我不知道为什么它的表现不符合我的想法。非常感谢任何帮助!
答案 0 :(得分:0)
就个人而言,我想我会做的:
namespace :admin do
resources :users, :except => :destroy do
delete :delete_contacts, :on => :member
end
end
哪会给你:
delete_contacts_admin_user DELETE /admin/users/:id/delete_contacts(.:format) admin/users#delete_contacts
然后,你可以这样做:
<%= link_to delete_contacts_admin_user_path(@user) ... %>
我的想法是,您正在使用特定的user
,因此使用member
路径而不是collection
路径,IMO似乎更自然。
当然,你必须稍微改变你的行动:
class Admin::UsersController < ApplicationController
...
def delete_contacts
user = User.find(params[:id])
user.contacts.destroy_all
redirect_to edit_admin_user_path(current_user.id)
flash[:notice] = "Successfully deleted #{user.name} contacts"
end
...
end
基于此edit_admin_user_path
可能会被提及,因此您可能也必须查看该路线(也可能是其他路线)。