我对省的搜索位置采取了这样的行动:
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 %>;
但是对象在输出上有引号:
[{"id":0,text:"a"},{"id":1,text:"b"}]
我希望输出如下:
[{id:0,text:"a"},{id:1,text:"b"}]
答案 0 :(得分:3)
试试这个:
var data = <%= raw(@location_options.to_json) %>;
或者:
var data = <%= @location_options.to_json.html_safe %>;