在以下情况之前无法将Hash转换为JSON:show action

时间:2014-11-17 17:40:48

标签: ruby-on-rails ruby json

我尝试在页面上查看之前将Hash转换为JSON,但不能转换变量类型。控制台(rails c)中的示例:

2.1.5 :001 > @item = Item.find(2)
  Item Load (1.6ms)  SELECT  "items".* FROM "items"  WHERE "items"."id" = $1 LIMIT 1  [["id", 2]]
 => #<Item id: 2, nested_params: {"123"=>"123", "456"=>"456"}> 
2.1.5 :003 > @item.nested_params.class
 => Hash 
2.1.5 :004 > @item.nested_params = @item.nested_params.to_json
 => "{\"123\":\"123\",\"456\":\"456\"}" 
2.1.5 :005 > @item.nested_params.class
 => Hash 
2.1.5 :006 > @item.nested_params
 => {}

1 个答案:

答案 0 :(得分:2)

您可以访问以下链接 http://www.rexfeng.com/blog/2012/12/convert-a-ruby-hash-into-valid-json/

我认为你必须写下要求&#39; json&#39;