Redis + Devise 配置混乱

Redis + Devise configuration confusion

我最近将我的 Rails 网络应用程序与 Redis/Sidekiq 集成在一起。在尝试了大量配置之后,我意识到我可能搞砸了,因为我无法再登录到我的应用程序。在参考了这个问题后:Devise: Suddenly cannot log in anymore 我认为它与我的 config/initializers/session_store.rb 有关,但我想因为我不清楚该文件究竟是如何操作的,所以我很难调试这个错误。

这是我尝试使用良好凭据登录时的实际错误消息:

ActionController::UrlGenerationError in Devise::SessionsController#create
No route matches {:action=>"show", :controller=>"controllers"} missing required keys: [:id]

这是我的 session_store.rb:

Rails.application.config.session_store :cookie_store, key: '_appname_session'
AppName::Application.config.session_store :redis_store, servers: "redis://localhost:6379/0/session"

这是我的 routes.rb 文件:

Rails.application.routes.draw do
  resources :controllers
  devise_for :users 
  require 'sidekiq/web'
  mount Sidekiq::Web => '/sidekiq'
end

引用此 link:What is called session store? 我知道会话存储只是会话信息的存储,但我不确定它是如何转化为这个设计错误的。

我也有合适的设计初始化文件,在我的 config/environments/development.rb 我有

  config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }

根据文档。

设计初始化程序:

  config.mailer_sender = 'please-change-me-at-config-initializers-devise@example.com' #haven't yet had the need to change this
  config.secret_key = 'secret_key'
  require 'devise/orm/active_record'
  config.case_insensitive_keys = [:email]
  config.strip_whitespace_keys = [:email]
  config.skip_session_storage = [:http_auth]
  config.stretches = Rails.env.test? ? 1 : 11
  config.reconfirmable = true
  config.expire_all_remember_me_on_sign_out = true
  config.password_length = 6..128
  config.email_regexp = /\A[^@\s]+@[^@\s]+\z/
  config.reset_password_within = 6.hours
  config.sign_out_via = :delete
end

Controller 'controllers'--> 这对我来说真的很奇怪。我不记得自己做过这个,我不确定它是否是我遵循的其他过程的副产品,但不知何故最终出现在我的申请中。在下方找到:

    class ControllersController < ApplicationController
  before_action :set_controller, only: [:show, :edit, :update, :destroy]

  # GET /controllers
  # GET /controllers.json
  def index
    @controllers = Controller.all
  end

  # GET /controllers/1
  # GET /controllers/1.json
  def show
  end

  # GET /controllers/new
  def new
    @controller = Controller.new
  end

  # GET /controllers/1/edit
  def edit
  end

  # POST /controllers
  # POST /controllers.json
  def create
    @controller = Controller.new(controller_params)

    respond_to do |format|
      if @controller.save
        format.html { redirect_to @controller, notice: 'Controller was successfully created.' }
        format.json { render :show, status: :created, location: @controller }
      else
        format.html { render :new }
        format.json { render json: @controller.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /controllers/1
  # PATCH/PUT /controllers/1.json
  def update
    respond_to do |format|
      if @controller.update(controller_params)
        format.html { redirect_to @controller, notice: 'Controller was successfully updated.' }
        format.json { render :show, status: :ok, location: @controller }
      else
        format.html { render :edit }
        format.json { render json: @controller.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /controllers/1
  # DELETE /controllers/1.json
  def destroy
    @controller.destroy
    respond_to do |format|
      format.html { redirect_to controllers_url, notice: 'Controller was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_controller
      @controller = Controller.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def controller_params
      params.require(:controller).permit(:Reminder)
    end
end

非常感谢帮助调试此错误!

首先,您不需要配置 session_store 即可使用 redis。

其次,您在session_store.rb中配置了两次。

我建议你使用 gem redis-rails 轻松配置 redis: https://github.com/redis-store/redis-rails