[英]Rails not decoding array of hashes from jQuery correctly
In my view I save an array of hashes in a hidden field 在我看来,我在隐藏字段中保存了一系列哈希
<div>
<%= hidden_field_tag :data_filtered, :value => @data_filtered %>
</div>
My javascript is 我的JavaScript是
$(document).ready(function(){
freezeTopRow($('#dataTable'));
$("#export").click(function(){
var data1 = $("#data_filtered").val()
$.ajax({
type: "POST",
url: "export",
dataType: 'json',
data: {data_filtered: data1}
});
});
});
My controller is 我的控制器是
def export
CSV.open("data.csv", "wb") do |csv|
csv << params[:data_filtered].first.keys
@data_filtered.each do |hash|
csv << hash.values
end
end
end
When I view params[:data_filtered] after it has been returned to the controller i see a string: 当我将params [:data_filtered]返回到控制器后,我看到一个字符串:
"{:value=>[{"a"=>nil, "b"=>nil, "c"=>0}]}"
But I want it to be in its original form of (array of hashes, in this case just 1 hash) half the problem is the :value. 但我希望它采用原始形式(哈希数组,在本例中为1个哈希),一半的问题是:value。 I don't want that to be stored and I don't know how to parse that to get just the array.
我不希望将其存储,也不知道如何解析该数组以获取数组。 Basically i want
基本上我想要
[{"a"=>nil, "b"=>nil, "c"=>0]
Try to send json data instead of string(that contains hash). 尝试发送json数据而不是字符串(包含哈希值)。 If you send string via ajax then in controller you have to
eval
the params which has SEVERE SECURITY issue. 如果您通过ajax发送字符串,则必须在控制器中
eval
存在严重安全问题的参数。
using eval
: (PLEASE DONT USE IT) 使用
eval
:(请不要使用)
eval('{:value=>[{"a"=>nil, "b"=>nil, "c"=>0}]}') will produce the original hash: {:value=>[{"a"=>nil, "b"=>nil, "c"=>0}]}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.