部署Rails应用遇LoadError:无法加载I18n文件求助
Hey, I’ve run into similar issues when precompiling assets for Rails 5.x apps—let’s walk through fixing both the deprecation warning and the LoadError step by step.
Fixing the Erubis Deprecation Warning
This warning pops up because Rails 5.1 started phasing out Erubis in favor of Erubi, a faster, better-maintained alternative. To resolve it:
- Open your
config/application.rbfile. - Inside the
class Application < Rails::Applicationblock, add this line (you can place it right afterconfig.load_defaults 5.1if that’s present):config.action_view.template_handlers[:erb] = ActionView::Template::Handlers::ERB::Erubi - Save the file—this tells Rails to use Erubi instead of Erubis, and the warning should disappear on your next precompile run.
Resolving the I18n LoadError
This error typically comes from missing dependencies, invalid locale files, or misconfigured I18n settings. Try these troubleshooting steps in order:
Verify the i18n gem is installed correctly
Runbundle show i18nin your terminal. If it returns an error saying the gem isn’t found, re-runbundle installto ensure all production dependencies are installed. For production environments, usebundle install --deploymentto lock dependencies to the versions in your Gemfile.lock.Check for YAML syntax errors in locale files
Invalid YAML in yourconfig/locales/directory is a common culprit. Even a missing space after a colon or mismatched quotes can break locale loading. Validate your YAML files with this command (replaceen.ymlwith each locale file in the directory):ruby -e "require 'yaml'; YAML.load_file('config/locales/en.yml')"If a file has syntax issues, this command will throw an error pointing to the problem line.
Double-check I18n configuration
Openconfig/application.rbandconfig/environments/production.rbto confirm your settings are correct:- Ensure
config.i18n.load_pathisn’t pointing to non-existent files. - Verify
config.i18n.default_localeis set to a valid locale (like:en). - Remove any custom I18n initializers you recently added if they might be overriding default behavior.
- Ensure
Ensure compatibility between activesupport and i18n
Rails 5.1.6 requires a specific range of i18n versions (usually ~> 0.7 or ~> 0.9). Check your Gemfile.lock to see which version you’re using. If it’s incompatible, add a version constraint to your Gemfile:gem 'i18n', '~> 0.9.5'Then run
bundle update i18nto install a compatible version.Confirm your Ruby and bundle environment match
Sometimes the error happens if you’re using a different Ruby version than the one your bundle was installed with. Runruby -vandbundle exec ruby -v—they should show the same version. If not, switch to the correct Ruby version (using rbenv, rvm, or chruby) and re-runbundle install.
Once you’ve addressed both issues, try running the precompile command again:
bundle exec rake assets:precompile RAILS_ENV=production
内容的提问来源于stack exchange,提问作者Ray Wojciechowski

