[英]Ruby routes and custom action
我正在嘗試將Devise和Cancan整合到網絡應用中。 我希望具有:role =>“ admin”的用戶能夠刪除用戶,並且Devise的destroy操作僅允許用戶刪除自己,因此我為此創建了一個自定義操作。 (要覆蓋插件的控制器文件,我已將注冊控制器復制到app / controllers / registrations_controller.rb。)
這是我的registrations_controller.rb中的自定義操作:
def destroy_user_account
@user = User.find_by_id(params[:user])
@user.destroy
redirect_to profiles_path, :flash => { :success => "User deleted!" }
authorize! :destroy, User, :message => "You don't have authorisation to delete this user."
end
在您查看用戶個人資料的頁面上的鏈接中,這就是我嘗試使用它的方式。 (我已經進行了設置,以便每個用戶都有has_one個人資料;個人資料就是您在前端看到的內容。個人資料會在用戶注冊時自動在個人資料表中創建。)
<% if can? :update, @profile %>
| <%= link_to 'Edit Profile', edit_profile_path(@profile) %>
| <%= link_to 'Edit Settings', edit_settings_path %>
<% end %>
<% if can? :destroy, @profile.user %>
| <%= link_to "Delete User", destroy_user_account(@profile.user),
:class => "delete",
:confirm => "Are you sure?",
:title => "Delete #{@profile.user.name}"
%>
<% end %>
我的測試顯示2個無法解決的故障:
1)以管理員身份登錄時,ProfilesController GET show應該具有編輯配置文件的鏈接失敗/錯誤:get:show,:id => @profile ActionView :: Template :: Error:undefined method
destroy_user_account' for #<#<Class:0x105b474a8>:0x1057f32e8> # ./app/views/profiles/show.html.erb:41:in
viewsdestroy_user_account' for #<#<Class:0x105b474a8>:0x1057f32e8> # ./app/views/profiles/show.html.erb:41:in
___917863454_2195331000_0'#./spec/destroy_user_account' for #<#<Class:0x105b474a8>:0x1057f32e8> # ./app/views/profiles/show.html.erb:41:in
profiles_controller_spec.rb:1432)以管理員身份登錄時,ProfilesController GET顯示應具有刪除用戶帳戶的鏈接(使用注冊控制器中的destroy_user_account操作)失敗/錯誤:get:show,:id => @profile ActionView :: Template ::錯誤:
destroy_user_account' for #<#<Class:0x105b474a8>:0x105806d20> # ./app/views/profiles/show.html.erb:41:in
viewsdestroy_user_account' for #<#<Class:0x105b474a8>:0x105806d20> # ./app/views/profiles/show.html.erb:41:in
___917863454_2195331000_0'#。/ spec /destroy_user_account' for #<#<Class:0x105b474a8>:0x105806d20> # ./app/views/profiles/show.html.erb:41:in
profiles_controller_中的未定義方法destroy_user_account' for #<#<Class:0x105b474a8>:0x105806d20> # ./app/views/profiles/show.html.erb:41:in
另外,當我在瀏覽器中試用時,單擊“刪除用戶”鏈接會顯示以下錯誤:
路由錯誤
沒有路由匹配“ / destroy-user-account / 2”
以下是應該涵蓋的路線:
devise_for:users,#:path =>'',:skip => [:confirmations,:passwords,:registrations],:controllers => {:registrations =>“ registrations”}做
# Routes for ACCOUNT REGISTRATIONS get "join", :to => "registrations#new", :as => :new_user_registration post "join", :to => "registrations#create", :as => :user_registration get "settings/account", :to => "registrations#show", :as => :settings get "settings/account/edit", :to => "registrations#edit", :as => :edit_settings put "settings/account", :to => "registrations#update", :as => :update_settings delete "close-my-account/:id", :to => "registrations#destroy", :as => :close_my_account delete "destroy-user-account/:id", :to => "registrations#destroy_user_account", :as => :destroy_user_account
誰能幫我解決我做錯的事情?
在瀏覽器中,它與路由不匹配,因為您正在發送GET
請求,但路由僅與DELETE
請求匹配。 測試失敗,因為路由的名稱為destroy_user_account_path
而不是destroy_user_account
。
嘗試:
<%= link_to "Delete User", destroy_user_account_path(@profile.user),
:class => "delete",
:confirm => "Are you sure?",
:title => "Delete #{@profile.user.name}"
:method => :delete
%>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.