[英]What's the best way to allow users to input fields that will be serialized as hashes or arrays?
I have a field that is stored like this: 我有一个这样存储的字段:
serialize :value
And, I'd like to store it as an array/hash/string depending on the input format. 而且,我想根据输入格式将其存储为数组/哈希/字符串。 Is there an accepted best practice about how to handle this?
是否存在有关如何处理此问题的最佳实践? The input will always come from the user.
输入将始终来自用户。
If I understand your problem correctly, you are talking about the opposite of serialization, which is parsing or tokenzing (converting a string into meaningful pieces)... That's a very generic problem, but take a look at the String.match
and String.scan
to get you started. 如果我正确地理解了您的问题,那么您在谈论的是串行化的对立面,即解析或标记化(将字符串转换为有意义的片段)...这是一个非常普通的问题,但请看一下
String.match
和String.scan
以开始使用。 Here are some stack overflow quetions that could be helpful (remember to search for things like this first!) 这是一些可能有用的堆栈溢出异常(请记住首先搜索类似的东西!)
What's the "ruby way" to parse a string for a single key/value? 解析单个键/值的字符串的“红宝石方式”是什么?
The best practice is to serialize Arrays and Hashes. 最佳实践是序列化数组和哈希。 When it comes to string there is no need for serialization.
当涉及字符串时,不需要序列化。 HTML form elements like checkboxes posts the array, which you can be serialized and saved directly in table.
HTML表单元素(如复选框)发布了数组,您可以对其进行序列化并直接保存在表中。 For Hashes, you should manipulate yourself accordingly and apply to the attribute.
对于哈希,您应该相应地进行操作并应用于属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.