简体   繁体   English

Ruby路由和自定义操作

[英]Ruby routes and custom action

I'm trying to incorporate Devise and Cancan into a web app. 我正在尝试将Devise和Cancan整合到网络应用中。 I want users with :role => "admin" to be able to delete users, and Devise's destroy action only allows users to delete themselves, so I've created a custom action for this purpose. 我希望具有:role =>“ admin”的用户能够删除用户,并且Devise的destroy操作仅允许用户删除自己,因此我为此创建了一个自定义操作。 (To override the plugin's controller file, I've copied the registrations controller over to app/controllers/registrations_controller.rb.) (要覆盖插件的控制器文件,我已将注册控制器复制到app / controllers / registrations_controller.rb。)

Here is my custom action in my 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

Here is how I'm trying to use it, in a link on the page where you view a user's profile. 在您查看用户个人资料的页面上的链接中,这就是我尝试使用它的方式。 (I have things set up so that each user has_one profile; profiles are what you see at the front end. A profile is automatically created in the profiles table on user registration.) (我已经进行了设置,以便每个用户都有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 %>   

My tests are showing 2 failures that I can't resolve: 我的测试显示2个无法解决的故障:

1) ProfilesController GET show when signed in as an admin should have a link to edit the profile Failure/Error: get :show, :id => @profile ActionView::Template::Error: undefined method destroy_user_account' for #<#<Class:0x105b474a8>:0x1057f32e8> # ./app/views/profiles/show.html.erb:41:in _app_views_profiles_show_html_erb___917863454_2195331000_0' # ./spec/controllers/profiles_controller_spec.rb:143 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 views destroy_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:143

2) ProfilesController GET show when signed in as an admin should have a link to delete the user's account (using the destroy_user_account action in the registrations controller) Failure/Error: get :show, :id => @profile ActionView::Template::Error: undefined method destroy_user_account' for #<#<Class:0x105b474a8>:0x105806d20> # ./app/views/profiles/show.html.erb:41:in _app_views_profiles_show_html_erb___917863454_2195331000_0' # ./spec/controllers/profiles_controller_spec.rb:148 2)以管理员身份登录时,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 views destroy_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

Also, when I try it out in my browser, clicking on the "Delete user" link gets me the following error: 另外,当我在浏览器中试用时,单击“删除用户”链接会显示以下错误:

Routing Error 路由错误

No route matches "/destroy-user-account/2" 没有路由匹配“ / destroy-user-account / 2”

Here are the routes that should cover this: 以下是应该涵盖的路线:

devise_for :users, #:path => '', :skip => [ :confirmations, :passwords, :registrations ], :controllers => { :registrations => "registrations" } do 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 

Can anyone help with what I'm doing wrong? 谁能帮我解决我做错的事情?

In the browser it isn't matching the route because you're sending a GET request but the route only matches a DELETE request. 在浏览器中,它与路由不匹配,因为您正在发送GET请求,但路由仅与DELETE请求匹配。 The test fails because the route gives the name destroy_user_account_path instead of destroy_user_account . 测试失败,因为路由的名称为destroy_user_account_path而不是destroy_user_account

Try: 尝试:

<%= 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM