views:

23

answers:

1

In my database, there are some text content, one of them is:

<% abc do %>
ddd
<% end %>

When I dumped it to yaml with to_yaml(), it is like:

content: |-
    <% abc do %>
    ddd
    <% end %>

And then, when I use rake db:fixtures:load, such error occurs:

The exact error was: 
  NoMethodError: undefined method 'abc' for main:Object 

I checked the source of db:fixtures:load, and found rails will not treat the content as plain text, but erb template, so it will try to find and execute the 'abc' method.

How can I fix this? I think the default 'to_yaml' should not be used.

+1  A: 

We can escape the '<%' as: http://stackoverflow.com/questions/2322413/how-do-i-escape-erb-code-in-fixtures

I'm doing now: before writing to file, I replaced all '<%' with '<%%'. But it is not always work correctly. If the content has some non-English charactor, the content will be dump as 'binary', not text. Then when read back, the '<%%' won't be converted to '<%'.

Freewind