简体   繁体   English

允许用户输入将序列化为哈希或数组的最佳方法是什么?

[英]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.matchString.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? 解析单个键/值的字符串的“红宝石方式”是什么?

How do I tokenize this string in Ruby? 如何在Ruby中标记该字符串?

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.

相关问题 数组和哈希之间有什么区别? - What's the difference between arrays and hashes? 用RSpec测试哈希数组的最佳方法是什么? - What is the best way to test array of hashes with RSpec? Rails:允许用户将图像上传到Dropbox链接文件夹或Amazon S3上“我们的”存储的最佳方法 - Rails: Best way to allow users to upload images to either a Dropbox linked folder or “our” storage on Amazon S3 允许网站用户编辑现有数据库记录的最佳方法是什么? - What is the best way to allow website users to edit already existing database records? 按此顺序组合2个哈希的最佳方法 - Best way to combine 2 hashes in this order 用图像字段实现用户友好表单的最佳方法是什么? - What's the best way to implement a user-friendly form with image fields? 在客户端上检测Rails 3.2表单的任何字段的任何更改的最佳方法是什么? - What's the best way to detect on client side any changes to any fields of a rails 3.2 form? 以不同的方式合并三个哈希的 arrays - Merge three arrays of hashes in different way 允许用户输入代码示例的最危险方法是什么? - What is the least dangerous way to allow users to enter code samples? 允许用户订购模型的最有效方法是什么? - What is the most efficient way to allow users to order a model?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM