1

我正在尝试将 Devise 和 Cancan 合并到一个网络应用程序中。我希望具有 :role => "admin" 的用户能够删除用户,而 Devise 的销毁操作只允许用户删除自己,因此我为此创建了一个自定义操作。(为了覆盖插件的控制器文件,我已将注册控制器复制到 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

这是我尝试使用它的方式,在您查看用户个人资料的页面上的链接中。(我设置了一些东西,以便每个用户都有一个配置文件;配置文件是您在前端看到的。配置文件会在用户注册时在配置文件表中自动创建。)

    <% 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 以管理员身份登录时应该有一个链接来编辑配置文件 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/控制器/profiles_controller_spec.rb:143

2) ProfilesController GET show 以管理员身份登录时应该有一个删除用户帐户的链接(使用注册控制器中的 destroy_user_account 操作)失败/错误:get :show, :id => @profile ActionView::Template::错误:未定义的方法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

此外,当我在浏览器中尝试时,单击“删除用户”链接会出现以下错误:

路由错误

没有路由匹配“/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

谁能帮助我做错了什么?

4

1 回答 1

0

在浏览器中,它与路由不匹配,因为您正在发送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
%>
于 2012-04-20T09:07:42.100 回答