【问题标题】:Redis + Devise configuration confusionRedis + Devise 配置混乱
【发布时间】:2016-07-11 17:27:40
【问题描述】:

我最近将我的 Rails webapp 与 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

引用此链接: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

不胜感激有关调试此错误的帮助!

【问题讨论】:

  • 您的应用程序/控制器中是否有类似ControllersController 的内容?您也可以发布您的设计初始化程序吗?
  • 我在上面的帖子中添加了这个信息!

标签: ruby-on-rails ruby devise redis sidekiq


【解决方案1】:

首先,你不需要配置你的 session_store 来使用 redis。

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

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

【讨论】:

  • 嗨特立独行,感谢您的意见。我实际上在我的应用程序中使用了 redis-rails 并从 gem 的 wiki 中提取了 session_store 配置: MyApplication::Application.config.session_store :redis_store, servers: "redis://localhost:6379/0/session" github.com/redis-store/redis-rails你介意详细说明我到底做错了什么吗?再次感谢!
  • 不需要配置session_store两次,只要使用cookie_storeredis_store,你能告诉我你的routes.rb中的resources :controllers是什么,你能把它去掉吗?
  • 当我删除 resources:controllers 和 redis_store 行时,一切正常。不知道到底出了什么问题,但感谢您的帮助!
猜你喜欢
  • 2013-10-29
  • 1970-01-01
  • 2011-01-13
  • 2017-05-22
  • 1970-01-01
  • 2015-12-13
  • 2014-06-15
  • 2019-09-06
  • 2013-01-15
相关资源
最近更新 更多