How to solve the “undefined method `deep_symbolize_keys’ for nil:NilClass” error in Rails

I was trying to migrate a table in my Rails app and got this error:
undefined method `deep_symbolize_keys' for nil:NilClass

It’s a strange error, and there weren’t very many hits on Google. A lighthouse ticket provided a clue.

The problem wasn’t one of my plugins or gems — it was i18n itself. Upgrading from 0.4.1 to 0.5.0 solved my problem. (I am using Rails 2.3.10.)

Here’s the full stack trace:

$ rake db:migrate RAILS_ENV=production –trace
(in /home/loranaw/apply/releases/20101210024212)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
rake aborted!
undefined method `deep_symbolize_keys’ for nil:NilClass
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/simple.rb:34:in `store_translations’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/base.rb:215:in `load_file’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/base.rb:215:in `each’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/base.rb:215:in `load_file’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/base.rb:21:in `load_translations’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/base.rb:21:in `each’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/base.rb:21:in `load_translations’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/simple.rb:57:in `init_translations’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/backend/simple.rb:40:in `available_locales’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n/config.rb:38:in `available_locales’
/usr/lib/ruby/gems/1.8/gems/i18n-0.4.1/lib/i18n.rb:35:in `available_locales’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:226:in `valid_locale?’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:243:in `parse_extensions’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:233:in `split’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:118:in `initialize’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:92:in `new’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:92:in `create_template’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:87:in `templates_in_path’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:86:in `each’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:86:in `templates_in_path’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/template.rb:69:in `load!’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/dependencies.rb:173:in `to_proc’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/paths.rb:40:in `each’
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.10/lib/action_view/paths.rb:40:in `load!’
/usr/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/initializer.rb:402:in `load_view_paths’
/usr/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/initializer.rb:194:in `process’
/usr/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/initializer.rb:113:in `send’
/usr/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/initializer.rb:113:in `run’
/home/loranaw/apply/releases/20101210024212/config/environment.rb:9
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require’
/usr/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/dependencies.rb:182:in `require’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/dependencies.rb:547:in `new_constants_in’
/usr/lib/ruby/gems/1.8/gems/activesupport-2.3.10/lib/active_support/dependencies.rb:182:in `require’
/usr/lib/ruby/gems/1.8/gems/rails-2.3.10/lib/tasks/misc.rake:4
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain’
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:607:in `invoke_prerequisites’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in `each’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:604:in `invoke_prerequisites’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:596:in `invoke_with_call_chain’
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run’
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in `load’
/usr/bin/rake:19

Fixing the mysterious “Element script is missing required attribute src” error

Suppose you're using the W3C validator to validate HTML5. Here's a sample document:

<!doctype html>
<html lang="en" xml:lang="en">
<head>
<meta charset="utf-8" />
<title>Hello</title>
<script type="text/javascript" charset="utf-8">
//<![CDATA[
var foo = 1;
//]]>
</script>
</head>
<body>
</body>
</html>

Everything looks right, but you get this mysterious error:

Line 6, Column 48: Element script is missing required attribute src.
<script type="text/javascript" charset="utf-8">

But you don't want or need an src attribute, because your JavaScript is inline. The solution is not to add the src attribute, but to remove the charset attribute.

(Note: a bug has been filed and this will be fixed in a future release.)