In my d开发者_JS百科atabase, 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.
We can escape the '<%' as: 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 '<%'.
精彩评论