当前位置:  开发笔记 > 编程语言 > 正文

在Rails中删除用户的帐户

如何解决《在Rails中删除用户的帐户》经验,为你挑选了1个好方法。

我有删除用户帐户的问题.这是我收到的错误:

undefined method `user_path' for #<#:0x00000005358f68>

我正在使用Devise + omniauth,所以对于像我这样的初学者来说,我的路由看起来有点复杂.

Rails.application.routes.draw do

  devise_for :users, :controllers => { omniauth_callbacks: 'omniauth_callbacks' }
  match '/users/:id/finish_signup' => 'users#finish_signup', via: [:get, :patch], :as => :finish_signup
  match 'users/:id' => 'users#show', via: :get
  match 'users' => 'users#index', via: :get
  root to: 'static_pages#about'

  get 'static_pages/help'

  resources :categories do
    resources :games, shallow: true
  end

  resources :games, only: [:show, :edit, :update, :destroy] do
    resources :comments
  end

end

"rake routes"输出:

                  Prefix Verb      URI Pattern                                  Controller#Action
        new_user_session GET       /users/sign_in(.:format)                     devise/sessions#new
            user_session POST      /users/sign_in(.:format)                     devise/sessions#create
    destroy_user_session DELETE    /users/sign_out(.:format)                    devise/sessions#destroy
 user_omniauth_authorize GET|POST  /users/auth/:provider(.:format)              omniauth_callbacks#passthru {:provider=>/twitter/}
  user_omniauth_callback GET|POST  /users/auth/:action/callback(.:format)       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)                      devise/registrations#cancel
       user_registration POST      /users(.:format)                             devise/registrations#create
   new_user_registration GET       /users/sign_up(.:format)                     devise/registrations#new
  edit_user_registration GET       /users/edit(.:format)                        devise/registrations#edit
                         PATCH     /users(.:format)                             devise/registrations#update
                         PUT       /users(.:format)                             devise/registrations#update
                         DELETE    /users(.:format)                             devise/registrations#destroy
           finish_signup GET|PATCH /users/:id/finish_signup(.:format)           users#finish_signup
                         GET       /users/:id(.:format)                         users#show
                   users GET       /users(.:format)                             users#index

用户控制器:

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy, :finish_signup]

  def index
    @users = User.all
  end

  # GET /users/:id.:format
  def show
    @user = User.find(params[:id])
  end

  # GET /users/:id/edit
  def edit
    # authorize! :update, @user
  end

  # PATCH/PUT /users/:id.:format
  def update
    # authorize! :update, @user
    respond_to do |format|
      if @user.update(user_params)
        sign_in(@user == current_user ? @user : current_user, :bypass => true)
        format.html { redirect_to @user, notice: 'Your profile was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: 'edit' }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  # GET/PATCH /users/:id/finish_signup
  def finish_signup
    # authorize! :update, @user
    if request.patch? && params[:user] #&& params[:user][:email]
      if @user.update(user_params)
        # @user.skip_reconfirmation!
        sign_in(@user, :bypass => true)
        redirect_to root_url, notice: 'Your profile was successfully updated.'
      else
        @show_errors = true
      end
    end
  end

  # DELETE /users/:id.:format
  def destroy
    # authorize! :delete, @user
    @user.destroy
    respond_to do |format|
      format.html { redirect_to root_url }
      format.json { head :no_content }
    end
  end

  private
    def set_user
      @user = User.find(params[:id])
    end

    def user_params
      accessible = [ :name, :email ] # extend with your own params
      accessible << [ :password, :password_confirmation ] unless params[:user][:password].blank?
      params.require(:user).permit(accessible)
    end
end

我不知道为什么"users_path"没有像往常一样工作,所以我把这行添加到routes.rb:

我试过通过routes.rb中的这一行删除用户的帐户来解决我的问题,但是这没有用.

match 'users/id:' => 'users#destroy', via: :delete

这是我删除用户帐户的按钮:

= link_to 'Delete user', user, method: :delete, data: { confirm: "You sure?" }, class: "btn btn-danger btn-sm"

用户没有"@"符号,因为它处于迭代循环中.



1> SteveTurczyn..:

删除这两行

  match 'users/:id' => 'users#show', via: :get
  match 'users' => 'users#index', via: :get

并用......替换它们

  resources :users, only: [:index, :show, :destroy]

这将为您提供用户所需的三条路线.

推荐阅读
地之南_816
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有