0
数组JSON阵列现在,我有这个我的表单里面:轨道4:创建里面输入
<%= f.input :document, collection: @documents, wrapper: false, label: false, input_html: {class: 'fleft mleft5'} %>
这将生产JSON数组:
[{
"document":"126"
}]
我的控制器create
下方法:
document: params[:ng_geofence][:document]
我的莫德尔
def as_hash(format=:google)
{
document: document
}
end
%w[document].each do |key|
define_method(key) do
value && JSON.parse(value)[key]
end
end
我的问题是如何让输出的是这样的:
[{
"document":
{
"id":"126"
}
}]
---- ----修订
如果我改变了我的形式:
<%= select_tag "ng_geofence[driver_notification][document][id]", options_from_collection_for_select(@documents, "id", "name"), include_blank: true %>
我收到此错误:757: unexpected token at '{"document"=>{"id"=>"126"}}'
可能因为"=>"
代替":"
是'document'模式?因为如果是的话,那么我认为我有一个更简单的解决方案。 – Finks
'{“document”:“126”}'不是数组,'[..]'这里是数组,'{..}'什么是json对象。 –
@Finks nope。其属性 – AmirolAhmad