【问题标题】:Adding customer action for devise为设计添加客户操作
【发布时间】:2015-02-19 23:51:46
【问题描述】:

我正在尝试将一个简单的自定义操作 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=>"cancelaccount", :controller=>"userregistrations", :id=>12}

如果我将 link_to 链接更改为:

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

错误是:

# 的未定义方法 `cancelaccount_path'

非常感谢任何帮助。

这是“rake routes”的结果:

          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

【问题讨论】:

  • 你可以在这里发布你的rake routes 输出吗?
  • 感谢 Rustam,我刚刚添加了结果

标签: ruby-on-rails ruby devise routing


【解决方案1】:

我不确定您的 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

【讨论】:

  • 感谢鲁斯塔姆。非常感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
相关资源
最近更新 更多