我的控制器中有以下方法
@content = Post.where(sub_category_id: id).select('content')
unless @content.nil?
@content.each do |content|
form_array = JSON.parse(content.content)
array_test << form_array.values
end
我的内容列的值为null
,因此我收到错误
JSON::ParserError (757: unexpected token at 'null'):
但是为什么除非循环不起作用。当列具有null
值时,如果要阻止它,则执行循环
修改-1
考虑我的数据库表中有两行
一个是{"Style":"coupe","Year":"2012","Color":"green"}
#这很好用
第二个是null
#但此失败
答案 0 :(得分:1)
你的@content是一个数组,并且可以包含多个元素,这意味着它可以包含具有单个元素的元素,其值也为null
,但是数组中有一个元素,所以在你的情况下你的病情会失败,应该是:
@content.each do |content|
unless content.nil?
form_array = JSON.parse(content.content) if content.content.present?
array_test << form_array.values
end
end