删除散列对象上的quot

时间:2014-10-01 11:05:50

标签: ruby-on-rails ruby

我对省的搜索位置采取了这样的行动:

def search_locations
    @locations = Location.where(:province_id => params[:province_id])

    @location_options = []
    @locations.each do |l|
      hash = {}
      hash[:id] = l.id
      hash[:text] = l.name.to_s
      @location_options.push(hash)
    end

    respond_to do |format|
      format.js
    end
end

我想在文件search_locations.js.erb

上写哈希
var data = <%= @location_options.to_json %>;

但是对象在输出上有引号:

[{&quot;id&quot;:0,text:&quot;a&quot;},{&quot;id&quot;:1,text:&quot;b&quot;}]

我希望输出如下:

[{id:0,text:"a"},{id:1,text:"b"}]

1 个答案:

答案 0 :(得分:3)

试试这个:

var data = <%= raw(@location_options.to_json) %>;

或者:

var data = <%= @location_options.to_json.html_safe %>;