You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Rails 6部署报错:bundler加载cap命令失败及参数错误

部署Rails 6.1.4.4应用至预发布环境的Capistrano报错问题

问题现象

将Rails 6.1.4.4应用部署到预发布环境时,执行deploy:check:linked_dirs步骤触发报错。核心错误为bundler无法加载cap命令,且i18n的translate方法参数不匹配(传入2个参数,预期0-1个)。当前环境:Capistrano 3.11.0、Ruby 3.0.4。

错误日志

bundler: failed to load command: cap (/Users/invozone-guest/.rbenv/versions/3.0.4/bin/cap)
/Users/invozone-guest/.rbenv/versions/3.0.4/lib/ruby/gems/3.0.0/gems/i18n-1.12.0/lib/i18n.rb:210:in `translate': wrong number of arguments (given 2, expected 0..1) (ArgumentError)

配置文件内容

deploy.rb

# config valid for current version and patch releases of Capistrano
lock "~> 3.11.0"

set :application, 'spree_demo'
set :repo_url, 'git@bitbucket.org:freshprep_devs/spree_demo.git'
set :branch, 'master'

set :rbenv_type, :system
set :rbenv_ruby, '3.0.4'

set :rbenv_custom_path, '/home/deploy/.rbenv/'

# Default branch is :master
ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }.call
set :use_sudo, true


# Default value for :pty is false
set :pty, true

task :sake do
  on roles(:app), in: :sequence, wait: 5 do
    within release_path do
      as :deploy do
        with rails_env: :staging do
          execute :rake, ENV['task'], "RAILS_ENV=staging"
        end
      end
    end
  end
end
namespace :deploy do
  desc 'Restart application'
  task :restart do
    on roles(:app), in: :sequence, wait: 5 do
      # Your restart mechanism here, for example:
      # execute :touch, release_path.join('tmp/restart.txt')
      execute 'sudo', '/etc/init.d/spree', 'restart'
    end
  end
  after :publishing, :restart

  after :restart, :clear_cache do
    on roles(:web), in: :groups, limit: 3, wait: 10 do
      # Here we can do anything such as:
      # within release_path do
      #   execute :rake, 'cache:clear'
      # end
    end
  end

end

staging.rb

role :app, %w{34.95.59.175}
role :web, %w{34.95.59.175}
role :db,  %w{34.95.59.175}



server '34.95.59.175', user: 'deploy', roles: %w{web app}

set :rbenv_custom_path, '/home/deploy/.rbenv'
set :deploy_to, '/home/deploy/apps/spree_demo'

解决方案

报错根源是i18n 1.12.0版本与Ruby 3.0+、Capistrano 3.11.x存在兼容性冲突:i18n 1.12.0修改了translate方法的参数签名,但旧版Capistrano仍在使用传统参数调用方式。

解决步骤:

  1. 在项目Gemfile中锁定i18n到兼容版本:
    gem 'i18n', '~> 1.11.0'
    
  2. 执行依赖更新:
    bundle update i18n
    
  3. 重新运行部署命令:
    cap staging deploy
    

额外优化建议:

  • 确保本地与服务器的Ruby版本统一(均为3.0.4)
  • 若服务器使用用户级rbenv,将deploy.rb中的set :rbenv_type, :system改为:user
  • 移除deploy.rb中重复的set :branch配置(保留固定值或动态询问语句其一即可)

内容的提问来源于stack exchange,提问作者Bilal Ahmed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 11:18:38