Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails “关于通过设计破坏会话”;不能';找不到使用';id'=签出;_Ruby On Rails_Ruby_Devise - Fatal编程技术网

Ruby on rails “关于通过设计破坏会话”;不能';找不到使用';id'=签出;

Ruby on rails “关于通过设计破坏会话”;不能';找不到使用';id'=签出;,ruby-on-rails,ruby,devise,Ruby On Rails,Ruby,Devise,我正在运行Rails 5.1.2和Ruby 2.4.0 我在ApplicationController中创建了一个名为require\u login的方法,用于检查用户是否按顺序登录。如果且仅当它们未登录时,才会转发到登录页面 问题是,当我注销它们时,会出现以下错误 ActiveRecord::RecordNotFound at /users/sign_out Couldn't find User with 'id'=sign_out 罪魁祸首是: def show @user = U

我正在运行Rails 5.1.2和Ruby 2.4.0

我在ApplicationController中创建了一个名为
require\u 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
从我的用户控制器

这是我的代码:

应用程序控制器

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
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
另外,
rake路由
输出以下内容:

                  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

如果一个人没有经过身份验证,我该怎么做才能将其转发到登录页面?我该如何破坏他们的会话,以便在他们注销时正确地转发到登录页面?

参考资料:用户
设计:用户
是冲突的

请尝试以下代码:

routes.rb 此外,在
config/initalizer/develope.rb
文件中


config.sign\u-out\u-via=:删除
改为
config.sign\u-via=:获取

你做的一切都是对的。唯一缺少的是
:method=>:delete
调用。这是特定于rails的,您需要rails ujs或较早版本的rails jquery_ujs才能使其工作

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

如果不添加此项,则
method::delete
调用将不起作用。如果您根本不想使用它,那么将“注销”更改为使用GET

更改或将其添加到您的config/initializers/designe.rb:

如果它仍然不工作,那么您的config/routes.rb文件中的顺序是错误的<代码>设计:用户
必须在
资源:用户
之前:

devise_for :users
resources :users

你可以发布你的
config/routes.rb
?显示你的路线。@m.simonborg routes.rb发布!对不起,谢谢!
rake routes
的输出可能会有帮助,因为它会导致
No route matches[GET]“/users/sign_out”
unfortunatelygoto
config/initializer/designe.rb
和change-line
config.sign_-out\u via=:delete
config.sign_-out\u via=:GET
No,它们没有冲突。您必须先使用
design_:users
,然后再使用
resources:users
。我在不同的应用程序上使用ruby 2.4.x和2.5.x以及rails 4.2.x到5.1.5。如果您使用webpacker进行资产编译,那么rails 5.1+就是这样,否则,
jquery\u ujs
方法仍然是一条路要走。对我来说,我需要这样做,我没有使用
注销/via=:get
,但我必须确保我的设计注销链接包括
方法::delete
ala
=链接到('Logout',destroy\u user\u session\u path,class:'nav\u link',方法::delete)
                  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
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
# for rails 5.1 or higher
//= require rails-ujs

# for rails 5.0 or lower
//= require jquery
//= require jquery_ujs
config.sign_out_via = :get
devise_for :users
resources :users