Rails error, could not parse YAML

After updating the gems, I did:

/home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:148:in `parse': couldn't parse YAML at line 182 column 9 (Psych::SyntaxError) from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:148:in `parse_stream' from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:119:in `parse' from /home/megas/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/psych.rb:106:in `load' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth/formatters/latex.rb:6:in `<module:LATEX>' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth/formatters/latex.rb:3:in `<top (required)>' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth.rb:21:in `require' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/redcloth.rb:21:in `<top (required)>' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/case_sensitive_require/RedCloth.rb:6:in `require' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/RedCloth-4.2.3/lib/case_sensitive_require/RedCloth.rb:6:in `<top (required)>' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `require' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:68:in `block (2 levels) in require' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `each' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:66:in `block in require' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `each' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler/runtime.rb:55:in `require' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/bundler-1.0.10/lib/bundler.rb:120:in `require' from /home/megas/Work/railscasts/config/application.rb:10:in `<top (required)>' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:28:in `require' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:28:in `block in <top (required)>' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:27:in `tap' from /home/megas/.rvm/gems/ruby-1.9.2-p136/gems/railties-3.0.3/lib/rails/commands.rb:27:in `<top (required)>' from script/rails:6:in `require' from script/rails:6:in `<main>' 

ruby-1.9.2-P136 rails 3.0.3

I tried to reinstall the gem RedCloth, it did not help, the system wants to use only version 4.2.3

Any idea how to fix this? Thanks

+76
yaml ruby-on-rails-3 redcloth
Feb 12 2018-11-12T00:
source share
29 answers

You have an invalid YAML code. I mean invalid for Psych (new ruby ​​YAML parser).

If you can’t (or don’t want) to fix your YAML code, try loading the old YAML parser (syck) by adding it at the beginning of config/boot.rb

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

It’s just a “quick and dirty” solution, I know

+164
Feb 24 '11 at 15:59
source share

My regular Rails 3 application also had this problem because I used a localized yaml file for date / time.

As you can see in this commit https://github.com/rails/rails/commit/dc94d81 this can be easily "fixed" by placing the array in separate lines.

  - order: [ :year, :month, :day ] 18 + order: 19 + - :year 20 + - :month 21 + - :day 
+49
Mar 16 '11 at 9:11
source share

A small adjustment to the answer of Pavel Raupakh, who, when launched from the directory, finds all * .yml files recursively in all subdirectories and checks the file. I ran it from the Rails root directory.

 require 'yaml' d = Dir["./**/*.yml"] d.each do |file| begin puts "checking : #{file}" f = YAML.load_file(file) rescue StandardError puts "failed to read #{file}: #{$!}" end end 
+20
Mar 24
source share

The root cause has been described in many places, and I will fail it again.

There are two default yaml yaml options, the new one is the one you should use. Syck is old, it is not supported and does not die, it is currently used as a rollback when there is no libyaml (usually not linux systems).

The important thing is you have an incorrect yaml somewhere . This is most likely in your translation files (I had lines without quotes with%). Just try loading all your yml files with YAML.load_file in the production window and you will see which one is broken.

+17
Jun 02 '11 at 17:59
source share

I had this problem because I used a tab instead of spaces

+13
Nov 12 '11 at 17:43
source share

Best to Fix Your YAML Files

Here's how to use irb , so you don't need a rail console, which probably doesn't work:

 require 'yaml' YAML::ENGINE.yamler = 'psych' YAML.load_file('config/locales/xxx.en.yml') 

you will get a good output stating where the problem is:

 Psych::SyntaxError: couldn't parse YAML at line 25 column 17 from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse' from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:148:in `parse_stream' from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:119:in `parse' from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:106:in `load' from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/lib/ruby/1.9.1/psych.rb:205:in `load_file' from (irb):10 from /home/xxx/.rvm/rubies/ruby-1.9.2-p290/bin/irb:16:in `<main>' 
+13
Jan 26 '12 at 18:09
source share

Absolutely fixing the yaml code doesn’t just "mask" the real problem, forcing YAMl to use "syck". I had the same problem and found incorrect yml expressions in my localization files. If you just use the old parser, you will not get the benefits of all working on the new parser elsewhere in your project.

+6
May 29 '11 at 6:28
source share

The problem with the original question was in RedCloth. I ran into the same problem and just updated the latest version of RedCloth pearls (currently 4.2.7). The problem was solved.

The above tips from Honza and FlyboyArt sound and you should fix any custom YAML that you have, but since RedCloth is so popular that most users who find this question who also use RedCloth should make sure their GemFile has added this line

 gem 'RedCloth', ">= 4.2.7" 
+6
Jun 03 2018-11-11T00:
source share

For others reading this, I got this error after creating a typo in my database configuration - /config/database.yml

+4
Jan 28 '12 at 23:45
source share

This is a problem with package 1.0.10: details here

Just try to calculate the package.

+3
Feb 14 '11 at 2:58 p.m.
source share

What fixed in my reason was a really distorted YAML translation file in:

 config/locales/bg.yml 

YAML bug fixed, and everything was fine. :-)

+2
Feb 23 2018-11-23T00:
source share

In my case, this is not a Bundle problem: (assuming Ruby 1.9)

  • Ruby defaults to "psych" (the newer and supported yaml library associated with the C: libyaml library) if libyaml is present
  • Otherwise, Ruby uses "syck" (old and not supported)
  • YAML :: ENGINE.yamler = 'syck' will force Ruby to use "syck" on the machine, where psych is also installed.

More info here: requires yaml not to use psych by default

+2
May 03 '11 at 7:46 am
source share

For those who are plaguing this problem, I just discovered that my database.yml is triggering this error because it did not have a space between the password keyword and the password. There is also an almost invisible error with database.yml, which worked without errors on an earlier version of rails.

+2
Nov 30 '11 at 7:08
source share

I had this problem. My problem was that I had an extra tab in my database.yml file.

+2
May 14 '12 at 16:43
source share

It seemed to me because I used r18n library in the Sinatra application that I am creating, and in my translation file I had the following:

 day: !!pl 0: 0 days 1: 1 day n: %1 days 

which used to work very well in an older project under Ruby 1.8.7 , but which failed under Ruby 1.9.3 .

The answer from @SB gave me the key I need to solve my problem. Newer YAML deviated from %1 . Some quick digging and experiment with irb , and now I know that the newer version of the YAML parser requires you to put quotes around the lines starting with %1 , so I just changed my translation to

 day: !!pl 0: 0 days 1: 1 day n: "%1 days" 

and voila - the disgusting error message has disappeared.

+2
Sep 19
source share

I manage to solve this problem by installing gem psych inside the group: development and: test.

 gem 'psych' 
+2
Jun 26 '13 at 7:50
source share

I had the same problem with ruby ​​1.9.2-p180, switching to 1.9.2-p290 solved this for me

+1
Sep 21 '11 at 14:53
source share

Although the answer given by @Vicvega may or may not work (did not test it), it contradicts the Rails and Ruby Common "Protocol Configuration" rules and should be treated with caution (and even more in collaboration), although the "in this case is small

so my vote goes (if I can vote) for those who suggested eliminating syntax errors in YAML files.

now ... to solve this error, for me it was a new error, I did not have the locale file that I defined as the default in Config/application.rb in my Config/locales directory

happy coding

+1
Oct 13 '11 at 17:03
source share

You need to check .yml files for error, I found a problem in my .yml database

+1
Nov 08 2018-11-11T00:
source share

I had a similar problem with a garbled YAML translation file. He used a variable before defining it. The following was incorrect:

 ... messages: ... <<: *errors_messages ... messages: &errors_messages ... 

It had to be changed to:

 ... messages: &errors_messages ... messages: ... <<: *errors_messages ... 

Then he began to work again.

+1
Mar 08 '12 at 11:05
source share

I got this error from trying to connect to a remote db with the password 'p@ssword' and realized that the psychologist does not like the symbol '@' . The database password has been changed and the problem has been resolved.

+1
Mar 09 2018-12-12T00:
source share

If you look like me and run into a project (inherited) with hundreds of appliances, a few lines of Ruby can save you hours:

 require 'yaml' d = Dir.new('test/fixtures/') d.each do |file| begin f = YAML.load_file('test/fixtures/' + file) rescue StandardError puts "failed to read test/fixtures/#{file}: #{$!}" end end 

Just put it in your Rails-root and run it, delete it when you're done.

+1
Mar 22 '12 at 21:16
source share

In my case, there were 2 questions.

  • As @stwienert mentioned, array representation was a problem.
  • One more thing: if String started with% {var}, I got a Parse exception. I had to change the lines accordingly so as not to start with% {var}

For example, if the string was

%{user_name} welcome to %{application_name} - This caused an error

To fix this, I had to change it to

 Hi, %{user_name} welcome to %{application_name} 

Hope this helps someone.

Hello,

Shardul.

+1
Apr 14 '12 at 15:57
source share

Well, just in case it helps ...
What I've done:
- select everything and copy from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/es.yml to the new es.yml using notepad ++
- I tried to see this new file using the NetBeans IDE text editor, I received a warning about a safe boot using utf8 (I can’t remember the exact text). Therefore, he did not open it with this text editor.
- switched local configuration /application.rb i18n
- when I loaded the irb page that I got, "I couldn’t parse the YAML in row 0 of column 0", referring to Psych.
- Went to IRB and uploaded a file with syck, it was fine; switched to the psyche and got the same error.

How I solved it:
- came back to copy the contents from https://github.com/svenfuchs/rails-i18n/tree/master/rails/locale/es.yml , but this time I pasted it into the newly created file using the netBeans editor.
- restarted webRick.
- the problem is resolved.
Regards,
Victor

+1
Jul 29 '12 at 21:37
source share

Remove unused databases with database.rb . If you are using MySQL and there is no PostgreSQL, then remove the PG databases.yml code from databases.yml .

+1
Sep 28
source share

Pshych analysis is absorbed into the core. I am not sure if this is an elegant solution, but I can solve this problem by deleting it.

 gem uninstall psych 
+1
Nov 16 '12 at 8:26
source share

I had a really weird problem because I had spaces. For example:.

 title: "NASA" 

Not working but

 title:"NASA" 

whether.

+1
Feb 26 '13 at 3:45
source share

For other people looking at this, I found a problem in rerun.txt that config/cucumber.yml caused in the Rails app. rerun.txt was configured to store the most recent test with a cucumber error, and somehow I entered strange characters for the cucumber test on the console.

It was hard to find. I wish I could see Glenn Rempe's answer back.

0
Nov 05 '13 at 20:18
source share

One possible reason: display values ​​in this context are not allowed in a string ...

Here is an incorrect YAML example (user: should not contain any value in fact, because it contains the children of some_key and some_other_key)

 customer: Customer user: User some_key: value some_other_key: value 2 

It is not a trivial task to find such a problem, especially if you have a huge YAML file.

I created a fairly simple regular expression to detect such things. I tested it in RubyMine

 ^(\s+)['"\w]+:\s?['"\w]+.*\n\1\s\s 

Be careful! This does not work correctly with special characters such as å ø æ etc.

Let me know in the comments if this works for you :)

0
Feb 20 '14 at 15:36
source share



All Articles