1

DeviseとCancanをWebアプリに取り込もうとしています。:role => "admin"のユーザーがユーザーを削除できるようにしたいのですが、Deviseの破棄アクションではユーザーが自分自身を削除することしかできないため、この目的のためにカスタムアクションを作成しました。(プラグインのコントローラーファイルをオーバーライドするために、registrationsコントローラーを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_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"} do

# 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 に答える