我在删除用户帐户时遇到问题。这是我收到的错误:
undefined method `user_path' for #<#<Class:0x0000000535a700>: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"
用户没有“@”符号,因为它处于迭代循环中。
答案 0 :(得分:6)
删除这两行
match 'users/:id' => 'users#show', via: :get
match 'users' => 'users#index', via: :get
并将其替换为......
resources :users, only: [:index, :show, :destroy]
这将为您提供用户所需的三条路线。
答案 1 :(得分:1)
要添加到@SteveTurczyn
的答案,您可以大量清理路线:
#config/routes.rb
devise_for :users, :controllers => { omniauth_callbacks: 'omniauth_callbacks' }
resources :users, only: [:index, :show, :delete] do
match :finish_signup, via: [:get, :patch], on: :member #-> url.com/users/:id/finish_signup
end
resources :static_pages, only: [] do
get :about, on: :collection #-> url.com/static_pages/about
end
resources :categories do
resources :games, shallow: true
end
resources :games, only: [:show, :edit, :update, :destroy] do
resources :comments
end
root to: 'static_pages#about'
-
您的link_to
等看起来不错