为设计添加客户操作

时间:2014-12-21 20:02:24

标签: ruby-on-rails ruby devise routing

我尝试向cancelaccount生成的用户模型添加一个简单的自定义操作devise,但很难获得正确的路由。

以下是 route.rb

中的内容
devise_for :users, controllers: { registrations: "userregistrations", sessions: "sessions" } do        
  get '/users/:id/cancelaccount', to: 'userregistrations#cancelaccount', as: 'cancelaccount'   
end

这是控制器

class UserregistrationsController < Devise::RegistrationsController      
  def create
  ...
  end

  def cancelaccount
      authenticate_user!
      Rails.logger.debug {"&& cancel"}
      @user = User.find(params[:id])
      unless (@user == current_user)
          redirect_to :back, :alert => "Access denied."
      end
  end
end

以下是HTML中的link_to行:

<li><%= link_to "Cancel Account", {controller: "userregistrations", action: "cancelaccount", id: current_user.id} %></li>

这是我得到的错误:

  

没有路线匹配{:action =&gt;&#34; cancelaccount&#34;,:controller =&gt;&#34; userregistrations&#34;,:id =&gt; 12}

如果我将link_to链接更改为:

<li><%= link_to "Cancel Account", cancelaccount_path(@user) %></li>

错误是:

  

未定义的方法`cancelaccount_path&#39;对于#&lt;#:0x00000006a5d4c0&gt;

非常感谢任何帮助。

以下是&#34; rake路线的结果&#34;:

          new_user_session GET      /users/sign_in(.:format)               sessions#new
              user_session POST     /users/sign_in(.:format)               sessions#create
      destroy_user_session DELETE   /users/sign_out(.:format)              sessions#destroy
   user_omniauth_authorize GET|POST /users/auth/:provider(.:format)        devise/omniauth_callbacks#passthru {:provider=>/(?!)/}
    user_omniauth_callback GET|POST /users/auth/:action/callback(.:format) devise/omniauth_callbacks#:action
             user_password POST     /users/password(.:format)              devise/passwords#create
         new_user_password GET      /users/password/new(.:format)          devise/passwords#new
        edit_user_password GET      /users/password/edit(.:format)         devise/passwords#edit
                           PATCH    /users/password(.:format)              devise/passwords#update
                           PUT      /users/password(.:format)              devise/passwords#update
  cancel_user_registration GET      /users/cancel(.:format)                userregistrations#cancel
         user_registration POST     /users(.:format)                       userregistrations#create
     new_user_registration GET      /users/sign_up(.:format)               userregistrations#new
    edit_user_registration GET      /users/edit(.:format)                  userregistrations#edit
                           PATCH    /users(.:format)                       userregistrations#update
                           PUT      /users(.:format)                       userregistrations#update
                           DELETE   /users(.:format)                       userregistrations#destroy
                      root GET      /                                      static_pages#home
                     users GET      /users(.:format)                       users#index
                           POST     /users(.:format)                       users#create
                  new_user GET      /users/new(.:format)                   users#new
                 edit_user GET      /users/:id/edit(.:format)              users#edit
                      user GET      /users/:id(.:format)                   users#show
                           PATCH    /users/:id(.:format)                   users#update
                           PUT      /users/:id(.:format)                   users#update
                           DELETE   /users/:id(.:format)                   users#destroy
           userpreferences GET      /userpreferences(.:format)             userpreferences#index
                           POST     /userpreferences(.:format)             userpreferences#create
        new_userpreference GET      /userpreferences/new(.:format)         userpreferences#new
       edit_userpreference GET      /userpreferences/:id/edit(.:format)    userpreferences#edit
            userpreference GET      /userpreferences/:id(.:format)         userpreferences#show
                           PATCH    /userpreferences/:id(.:format)         userpreferences#update
                           PUT      /userpreferences/:id(.:format)         userpreferences#update
                           DELETE   /userpreferences/:id(.:format)         userpreferences#destroy
                    signup GET      /signup(.:format)                      devise/registrations#new
              edit_profile GET      /edit_profile(.:format)                devise/registrations#edit
           change_password GET      /change_password(.:format)             devise/passwords#edit
                   sign_in GET      /sign_in(.:format)                     devise/sessions#new
                  sign_out GET      /sign_out(.:format)                    devise/sessions#destroy
              confirmation GET      /confirmation(.:format)                devise/confirmations#new
            unlock_account GET      /unlock_account(.:format)              devise/unlocks#new
                           GET      /signup(.:format)                      trainer/registrations#new
                      edit GET      /edit(.:format)                        trainer/registrations#edit
                           GET      /sign_in(.:format)                     devise/sessions#new
                           GET      /sign_out(.:format)                    devise/sessions#destroy
                           GET      /confirmation(.:format)                devise/confirmations#new
                           GET      /unlock_account(.:format)              devise/unlocks#new

1 个答案:

答案 0 :(得分:0)

我不确定您的devise_for阻止有什么问题,但您可以轻松地将此路线添加到其中:

devise_for :users, controllers: { registrations: "userregistrations", sessions: "sessions" }
match '/users/:id/cancelaccount' => 'registrations#cancelaccount', via: 'get', as: 'cancelaccount'

将添加您想要的路线:

cancelaccount GET    /users/:id/cancelaccount(.:format)                                       registrations#cancelaccount