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}