通过Devise销毁会话“无法找到具有'id'= sign_out的用户”

时间:2017-07-26 12:29:59

标签: ruby-on-rails ruby devise

我正在运行Rails 5.1.2和Ruby 2.4.0。

我在ApplicationController中创建了一个名为require_login的方法,用于检查用户是否按顺序登录。当且仅当他们没有登录时,他们才会被转发到登录页面。

问题在于,当我将它们签出时,我收到以下错误

ActiveRecord::RecordNotFound at /users/sign_out
Couldn't find User with 'id'=sign_out

罪魁祸首是:

def show
    @user = User.find(params[:id]) # right here
    authorize @user
end

来自我的UserController。

这是我的代码:

的ApplicationController

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_action :configure_permitted_parameters, if: :devise_controller?
  include Pundit

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:name])
    devise_parameter_sanitizer.permit(:account_update, keys: [:name])
  end

  def require_login
    unless signed_in?
      flash[:error] = "You must be logged in to access this section"
      redirect_to login_path # halts request cycle
    end
  end

end

UsersController

class UsersController < ApplicationController
  before_action :require_login

  def index
    @users = User.all
    authorize User
  end

  def show
    @user = User.find(params[:id])
    authorize @user
  end

  def update
    @user = User.find(params[:id])
    authorize @user
    if @user.update_attributes(secure_params)
      redirect_to users_path, :notice => "User updated."
    else
      redirect_to users_path, :alert => "Unable to update user."
    end
  end

  def destroy
    user = User.find(params[:id])
    authorize user
    user.destroy
    redirect_to users_path, :notice => "User deleted."
  end

  private

  def secure_params
    params.require(:user).permit(:role)
  end

end

我的布局/ topnav.html.erb ,有人可以退出

<div class="row border-bottom">
    <nav class="navbar navbar-static-top white-bg" role="navigation" style="margin-bottom: 0">
        <div class="navbar-header">
            <a class="navbar-minimalize minimalize-styl-2 btn btn-primary " href="#"><i class="fa fa-bars"></i> </a>
            <form role="search" class="navbar-form-custom" method="post" action="/">
                <div class="form-group">
                    <input type="text" placeholder="Search for something..." class="form-control" name="top-search" id="top-search" />
                </div>
            </form>
        </div>
        <ul class="nav navbar-top-links navbar-right">
            <li>
                <%= link_to 'Sign out', destroy_user_session_path, :method=>'delete' %>
            </li>
        </ul>
    </nav>
</div>

我的 routes.rb

Rails.application.routes.draw do
  root to: 'visitors#index'
  devise_for :users
  resources :users


  get "home/index"
  get "home/minor"
  root to: 'home#index'

end

同样rake routes输出以下内容:

                  Prefix Verb   URI Pattern                    Controller#Action
                    root GET    /                              visitors#index
        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
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
           user_password PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
                         POST   /users/password(.:format)      devise/passwords#create
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
       user_registration PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy
                         POST   /users(.:format)               devise/registrations#create
                   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
                    page GET    /pages/*id                     high_voltage/pages#show

如果某人未经过身份验证,我可以做什么才能转发到“登录”页面?我怎样才能销毁他们的会话,以便在他们退出时正确转发到登录页面?

2 个答案:

答案 0 :(得分:2)

你已经做好了一切。唯一缺少的是:method => :delete电话。这是特定于rails的,您需要 rails-ujs 或旧版rails jquery_ujs 才能使其正常工作。

将此添加到 app / assets / javascripts / application.js

# for rails 5.1 or higher
//= require rails-ujs

# for rails 5.0 or lower
//= require jquery
//= require jquery_ujs

如果您不添加此内容,method: :delete电话将无效。如果您根本不想使用它,请更改sign_out以使用 GET

更改或添加到 config / initializers / devise.rb

config.sign_out_via = :get

如果它仍然不起作用,那么您的 config / routes.rb 文件中的顺序错误。 devise_for :users必须在resources :users之前:

devise_for :users
resources :users

答案 1 :(得分:1)

resources :usersdevise_for :users是冲突的,所以

尝试以下代码:

的routes.rb

Rails.application.routes.draw do
  root to: 'visitors#index'
  devise_for :users
  #resources :users //comment the users resources because its already used by devise_for or used some other resources for users


  get "home/index"
  get "home/minor"
  root to: 'home#index'

end

此外,在您的config/initalizer/devise.rb文件中

config.sign_out_via = :delete更改为config.sign_out_via = :get