如何使用 ActiveRecord 的哈希数组序列化?
在我的模型 Post.rb 中,我有 serialize :facts
调用。 我试图实现的目标是用户可以将自己的键/值对保存到事实列中。当用户打开 /posts/new 页面时,我希望有三个空的键值输入。然后,用户可以填写如下值:
[color ]: [very blue ]
[length of the street ]: [not that long ]
[etcetera ]: [etcetera ]
为了构建表单,我正在使用 Simple Form gem 并已尝试使用
= f.simple_fields_for :facts do |ff|
= f.input :key
= f.input :value
它仅生成一个键/值,并将其以 YAML 格式保存在“事实”字段中!但是,当我尝试添加数组时,它不起作用。它仅保存最后一个键/值对。
好吧,我知道问题是什么,但不知道如何解决。
我需要一些可以生成输入字段的值,例如: post[facts][][key]
和 post[facts][][value]
。目前,它仅生成三个 post[facts][key]
post[facts][value]
输入。
In my model Post.rb I have serialize :facts
call.
What I try to accomplish is that users can save their own key/value pairs to the facts
column. When user opens /posts/new page, I want there to be three empty key-value inputs. User can then fill in values like:
[color ]: [very blue ]
[length of the street ]: [not that long ]
[etcetera ]: [etcetera ]
To build the form I'm using Simple Form gem and tried already to use
= f.simple_fields_for :facts do |ff|
= f.input :key
= f.input :value
It generates only one key/value and saves it very good in YAML format in 'facts'-field! However when I try to add an array it doesn't work. It saves only the last key/value pair.
Well, I know what problem is, but not how to solve it.
I need something that would generate input fields with values like: post[facts][][key]
and post[facts][][value]
. Right now it only generates three of post[facts][key]
post[facts][value]
inputs.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试将空对与集合中的现有元素合并?
Did you try to merge an empty pair with existing elements in the collection?