darko darko - 3 months ago 21
Ruby Question

Mongodb/mongoid rails project -- "did not find expected key while parsing a block mapping at line 8 column 7 (Psych::SyntaxError)"

I have a rails project which uses mongodb/mongoid and some additional mongodb/mongoid gems:

ruby "2.3.1"
gem 'rails', '4.1.1'
gem 'mongoid'


When I'm trying to run it, I get an exception:

$ bundle exec rails s
/home/alex/.gem/ruby/2.3.1/gems/activesupport-4.1.1/lib/active_support/values/time_zone.rb:285: warning: circular argument reference - now
=> Booting Thin
=> Rails 4.1.1 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
Exiting
/home/alex/.rubies/ruby-2.3.1/lib/ruby/2.3.0/psych.rb:377:in `parse': (<unknown>): did not find expected key while parsing a block mapping at line 8 column 7 (Psych::SyntaxError)
from /home/alex/.rubies/ruby-2.3.1/lib/ruby/2.3.0/psych.rb:377:in `parse_stream'
from /home/alex/.gem/ruby/2.3.1/gems/mongoid-4.0.2/lib/mongoid/config/environment.rb:40:in `load_yaml'
from /home/alex/.gem/ruby/2.3.1/gems/mongoid-4.0.2/lib/mongoid/config.rb:83:in `load!'
# ............skipped
from /home/alex/.gem/ruby/2.3.1/gems/railties-4.1.1/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/alex/.rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:228:in `block in tsort_each'
from /home/alex/.rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /home/alex/.rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /home/alex/.rubies/ruby-2.3.1/lib/ruby/2.3.0/tsort.rb:349:in `block in each_strongly_connected_component'
# .......... skipped
from /home/alex/.gem/ruby/2.3.1/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/alex/.gem/ruby/2.3.1/gems/activesupport-4.1.1/lib/active_support/dependencies.rb:247:in `require'
from /home/alex/projects/ruby/my_project_123/config.ru:3:in `block in <main>'
from /home/alex/.gem/ruby/2.3.1/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
# ............skipped
from /home/alex/.gem/ruby/2.3.1/gems/railties-4.1.1/lib/rails/commands.rb:17:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'


What can cause this exception?

Answer

Psych is a YAML parser and emitter. The error indicates somewhere in your yml files you have extra spaces (or not enough spaces) before a key value. Check all yml files (Ex: mongoid.yml )for proper syntax. You can validate your yml files Here. For example in the following YAML

key_one:
  inner:
  - value1
  - value2

key_two:
    inner:
  - value1
  - value2

line 7 column 5 has an extra space before a key value