我有一个用于登录,忘记密码和更改个人资料信息的用户身份验证系统,但我的登出路线对我来说很古怪并且给我错误The action 'destroy' could not be found for PagesController
。我不知道为什么它会调用PagesController。我有一个用于管理操作的命名空间PagesController和一个仅用于show
方法的页面控制器。就像我说的,除了会话注销外,一切都很完美。谢谢。
路由
Rails.application.routes.draw do
get 'password_resets/new'
get 'password_resets/edit'
get 'sessions/new'
get '/signup', to: 'users#new'
get '/login', to: 'sessions#new'
post '/login', to: 'sessions#create'
get '/logout', to: 'sessions#destroy'
resources :users
resources :account_activations, only: [:edit]
resources :password_resets, only: [:new, :create, :edit, :update]
get 'contact-me', to: 'messages#new', as: 'new_message'
post 'contact-me', to: 'messages#create', as: 'create_message'
resources :articles
mount ImageUploader.upload_endpoint(:cache) => '/images/upload'
mount StripeEvent::Engine, at: '/webhooks/stripe'
namespace :admin do
get '', to: 'dashboard#index', as: '/'
resources :pages
resources :articles
resources :categories
resources :tags
resources :albums
resources :banners
resources :products do
resources :variations
end
end
resources :products do
resources :variations
end
root 'pages#show', defaults: { id: 'home' } # shows home page as default from pages controller
resources :pages, path: ''
end
退出链接
<%= button_to 'Logout', logout_path, method: :delete %>
sessions_controller
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
if user.activated?
log_in user
params[:session][:remember_me] == '1' ? remember(user) : forget(user)
redirect_back_or user
else
message = "Account not activated. "
message += "Check your email for the activation link."
flash[:warning] = message
redirect_to root_url
end
else
flash.now[:danger] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
log_out if logged_in?
redirect_to root_url
end
end
sessions_helper
module SessionsHelper
# Logs in the given user.
def log_in(user)
session[:user_id] = user.id
end
# Remembers a user in a persistent session.
def remember(user)
user.remember
cookies.permanent.signed[:user_id] = user.id
cookies.permanent[:remember_token] = user.remember_token
end
# Returns true if the given user is the current user.
def current_user?(user)
user == current_user
end
# Returns the user corresponding to the remember token cookie.
def current_user
if (user_id = session[:user_id])
@current_user ||= User.find_by(id: user_id)
elsif (user_id = cookies.signed[:user_id])
user = User.find_by(id: user_id)
if user && user.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
# Returns true if the user is logged in, false otherwise.
def logged_in?
!current_user.nil?
end
# Forgets a persistent session.
def forget(user)
user.forget
cookies.delete(:user_id)
cookies.delete(:remember_token)
end
# Logs out the current user.
def log_out
forget(current_user)
session.delete(:user_id)
@current_user = nil
end
# Redirects to stored location (or to the default).
def redirect_back_or(default)
redirect_to(session[:forwarding_url] || default)
session.delete(:forwarding_url)
end
# Stores the URL trying to be accessed.
def store_location
session[:forwarding_url] = request.original_url if request.get?
end
end
答案 0 :(得分:2)
试试这个,
delete '/logout', to: 'sessions#destroy'
并使用
重新检查您的路线rake routes | grep logout
希望有所帮助
答案 1 :(得分:1)
您可以将method
get
更改为delete
delete '/logout', to: 'sessions#destroy'