简体   繁体   English

Ruby on Rails:如何将具有属性的数据库条目转换为人类可读的格式?

[英]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 (ie in the "revisions" column; ie @audit.revisions) 我正在使用acts_as_auditable插件,而revisions属性为我提供了这一块文本(即在“revisions”列中;即@ audit.revisions)

--- 
user_id: 2
kind: French
name: Delicious 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 (ie, @audit.revisions) into something like: 例如,我想将上面的块(即@ audit.revisions)转换为:

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: 如果您的修订列是String,那么您可以将任何YAML转换为哈希:

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. 更新 :我说上面的“哈希”,但它将转换为适合给定YAML的类型。 So it can be an array or object. 所以它可以是一个数组或对象。 But in your case it will be hash. 但在你的情况下,它将是哈希。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM