Rails4表单_中的JSON数据类型
如何实现将JSON PostgreSQL类型的值填充到rails中的嵌套表单中 我有一个这样的模特Rails4表单_中的JSON数据类型,json,ruby-on-rails-4,rails-postgresql,Json,Ruby On Rails 4,Rails Postgresql,如何实现将JSON PostgreSQL类型的值填充到rails中的嵌套表单中 我有一个这样的模特 title :text description :text grid :json 在属性内部,我想存储一个维度和其他东西 { "cols": 15, "rows": 15 "pos": { "x": 10, "y": 5 } } 对应形式为 @form_for @product do |f| f.text_field :title f.te
title :text
description :text
grid :json
在属性内部,我想存储一个维度和其他东西
{
"cols": 15,
"rows": 15
"pos": {
"x": 10,
"y": 5
}
}
对应形式为
@form_for @product do |f|
f.text_field :title
f.text_field :description
f.fields_for :grid do |grid_f|
grid_f.text_field :cols
grid_f.text_field :rows
end
end
但是列和列都没有填写。我是否必须手动创建输入并手动设置值。还是因为@product.grid中没有符号,只有字符串
因此@product.grid[:cols]不起作用,但@product.grid['cols']起作用。我认为formhelper使用模式生成其字段,并且由于hstore中的项目可能会有所不同,因此无法自动生成这些字段 除了实现细节之外,您还可以迭代对象()中的值:
{:value=>value}%>
<% f.grid.attributes.try(:each) do |key, value| %>
<%= f.text_field key, :input_html => {:value => value } %>
<% end %>