Ruby on Rails: How do you convert a database entry with attributes inside into a human-readable format?
I'm using the acts_as_auditable plugin, and the revisions attribute gives me this chunk of text (i.e. in the "revisions" column; i.e. @audit.revisions)
---
user_id: 2
kind: French
name: D开发者_如何转开发elicious Pies
I'm trying to convert that text into a human-readable format to display the details of the audit. For instance, I want to convert the above chunk (i.e, @audit.revisions) into something like:
Created "Delicious Pies", which is a French cuisine.
I'm thinking something along the lines of:
Created "<%= @audit.revisions[:name] %>", which is a <%= @audit.revisions[:kind] %> cusine.
Of course, that seems to be incorrect. Any ideas on doing it correctly?
If your revision column is a String then you can convert any YAML to hash with that:
y = YAML.load(@audit.revisions)
y["kind"] # => "French"
update: I said "hash" above but it will be converted to type that is appropriate for the given YAML. So it can be an array or object. But in your case it will be hash.
精彩评论