HTML code in Rails YAML-fixtures
HI, i have an "Article" model, that has HTML as it's content.
I have created some test data with YAML fixtures and faced following problem:
As i say "rake db:fixtures:load", following error appears:
ArgumentError: syntax error on line 22, col 0: <li>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
Fixtures fragment, it complains about, is following:
Article_1:
title: Test Article 1
content: |
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.</p>
<ul>
<li>Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</li>
<li>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.</li>
</开发者_如何学运维ul>
It seems, like the HTML code has something, that should not appear in YAML...
What is the proper way to write such a fixtures ?
I experienced a very similar error today when I needed to load a long JSON string in my fixture.
Unfortunately I wasn't able to fix it using Yaml but changing over to CSV did the trick. Perhaps that's something that you can try?
Instead of articles.yml you'll rename it to articles.csv. Keep in mind that the first line in the CSV file is a header record which needs to contain a comma separated list of field names after which the following lines can contain your data.
More details available over at http://ar.rubyonrails.org/classes/Fixtures.html which was a great help.
I just ran across something similar today embedding XML in a fixture. You should just be able to wrap your HTML in double quotes and it should work. Escape any double quotes in your HTML with an extra double quote.
You definitely need the | to handle multi-lines though because I was missing that at first so thanks for that tip.
精彩评论