The serialize method allows you to store the object as a hash in the database as a YAML string. However, I recently discovered that I want to have a text field that allows the user to enter their own string and have a controller that creates a hash of that string.
<%= f.text_field :yaml, :value => '--- \nlast_name: Smith\nfirst_name: Joe\n' %>
Yes, I want single quotes: I want to keep \ n on the display. But the problem is that as a result, the resulting string object receives the escape code:
I run the line through two regular expressions: the first replaces the double backslash with a single backslash. Then the next converts \ n (two characters) to \ n (special single character).
So, in my controller:
yhash = YAML.load(params[:form][:yaml].gsub(/\\\\/, "\\").gsub(/\\n/, "\n"))
Now it works, but it seems terribly confusing. Is there a more elegant way for the user to submit yaml?