Rails本地启动报错:Redis缓存存储参数数量错误(给定2,预期1)
Rails 6.1 + Ruby 3.0 配置Redis缓存参数错误解决
问题背景
环境:Rails 6.1.3.2、Ruby 3.0.0,已安装redis-rails '5.0.2',development.rb配置如下:
config.active_record.cache_versioning = false config.cache_store = :redis_store, "redis://localhost:6379/0/cache"
启动服务器后报错:wrong number of arguments (given 2, expected 1),错误栈指向Redis缓存存储的写入逻辑。
问题原因
redis-rails 5.0.2依赖的redis-activesupport 5.2.0与Ruby 3.0、Rails 6.1存在兼容性问题。Ruby 3.0对方法参数校验更严格,旧版本的Redis缓存实现调用Rails缓存初始化方法时,参数传递不符合新版本要求,导致参数数量错误。
解决办法
方法1:升级redis-rails版本
将redis-rails升级到兼容Ruby 3.0和Rails 6.1的版本,推荐~> 6.0:
- 修改Gemfile:
gem 'redis-rails', '~> 6.0'
- 执行bundle更新:
bundle install
新版本会引入适配的redis-activesupport,修复参数兼容问题。
方法2:调整缓存配置格式
若暂时无法升级gem,可改用哈希格式配置缓存地址:
config.cache_store = :redis_store, { url: "redis://localhost:6379/0/cache" }
这种写法能规避旧版本gem的参数处理bug。
验证
修改后重启Rails服务器,测试缓存相关功能,确认报错消失。
内容的提问来源于stack exchange,提问作者Vinoth Kumar
相关产品推荐
相关产品推荐

