Thursday, 1 December 2011

YAML parsing error in Rails 3 Solved

Here's another issue that costs some quality time of the developers.

The Problem : 
You have just created a Rails 3 application and went to the "config/database.yml" file and made some changes to it based on your requirements.
Nice Job !!
But wait, when you try to run the "$ > rake db:migrate" or "$ > rake db:create" or even generate a simple controller like "$ > rails g controller Images <options>", rails shows you an error message something like:


/home/souvikd/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse': couldn't parse YAML at line 8 column 11 (Psych::SyntaxError)
from /home/souvikd/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse_stream'
from /home/souvikd/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:119:in `parse'
from /home/souvikd/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:106:in `load'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/application/configuration.rb:106:in `database_configuration'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/railtie.rb:68:in `block (2 levels) in <class:Railtie>'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:36:in `execute_hook'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/lazy_load_hooks.rb:26:in `on_load'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activerecord-3.1.3/lib/active_record/railtie.rb:67:in `block in <class:Railtie>'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `instance_exec'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/initializable.rb:30:in `run'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/initializable.rb:55:in `block in run_initializers'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `each'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/initializable.rb:54:in `run_initializers'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/application.rb:96:in `initialize!'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /home/souvikd/image_gallery/config/environment.rb:5:in `<top (required)>'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `block in require'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `block in load_dependency'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:640:in `new_constants_in'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:223:in `load_dependency'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/activesupport-3.1.3/lib/active_support/dependencies.rb:240:in `require'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/application.rb:83:in `require_environment!'
from /home/souvikd/.rvm/gems/ruby-1.9.2-p290/gems/railties-3.1.3/lib/rails/commands.rb:22:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'


Solution :
Just add the following two highlighted lines to the first line of "config/boot.rb" file:

require 'yaml'
YAML::ENGINE.yamler = 'syck'

require 'rubygems'
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)
require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])



Then try to run the command you were planning to do.
It will work just fine.

No comments:

Post a Comment