我要在前端对数据(对象数组)进行字符串化,然后在后端对其进行解析。
运行规格时,出现错误smtp.newhostingcompany.com
如何在规范中对数据进行字符串化处理,以使其与前端中的操作保持一致?还是有另一种方式,我不必对数据进行字符串化/解析来处理所有这些问题?
这是我的前端数据结构的样子:
no implicit conversion of Array into String
在我的控制器中,我首先要验证数据是一个数组:
"categories_and_years": JSON.stringify(
[
{"category_id": 1, "year_ids":[1, 2, 3]},
{"category_id": 2, "year_ids":[2, 3]},
]
)
在我的规格中,我将这样设置参数:
def validate_categories_and_years_array
@cats_and_yrs = JSON.parse(params[:categories_and_years])
return unless @cats_and_yrs
if !@cats_and_yrs.is_a?(Array)
render_response(:unprocessable_entity, { description_detailed: "categories_and_years must be an array of objects"})
end
end
答案 0 :(得分:0)
这篇文章解释了规范中的常规红宝石哈希与HashWithIndifferentAccess之间的区别。
您还可以尝试to_json吗?
Params hash keys as symbols vs strings
PROCEDURE get_counts(
count1 OUT INT,
count2 OUT INT,
count3 OUT INT,
.. etc.
) IS
l_count1 INT;
l_count2 INT;
l_count3 INT;
.. etc.
BEGIN
SELECT COUNT(*) INTO l_count1 from table1 where condition_blah;
SELECT COUNT(*) INTO l_count2 from table1 where condition_blah;
SELECT COUNT(*) INTO l_count3 from table1 where condition_blah;
... etc
count1 := l_count1;
count2 := l_count2;
count3 := l_count3;
.. etc
END get_counts;