rails 前后端数据传递hash to json

controller.rb

@data = {a: 1, b: 2}
@data_new = {"a": 1, "b": 2}

index.html

var arr = <%= @data %>
#=> {:a =>1, :b =>2}

var arr = <%= raw @data %>
#=> arr = {:a => 1, :b => 2}

var arr = <%= raw @data_new %>
#=> arr = {:a => 1, :b => 2}

### 使用 gem “activesupport” stringify_keys

var arr = <%= raw @data_new.stringify_keys %> 
#=> arr = {"a" => 1, "b" => 2}

var arr = <%= raw @data_new.stringify_keys.to_json %> 
#=> arr = {"a": 1, "b": 2}



  

  

posted @ 2023-05-23 13:49  鞋带松了  阅读(14)  评论(0编辑  收藏  举报