[英]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.