这让我发疯
我需要链接格式为
links = [{source: 1, target: 2}, {..}, ...]
控制器代码:
@edges = []
Follow.where("followable_type= 'User'").map do |x|
@edges << {source: x.follower_id, target: x.followable_id}
end
查看代码(在 javascript 内):
var links = [(<%= @edges.to_json %>)];
html源代码中的输出是
var links = [([{"source":2,"target":3},{"source":2,"target":1},{"source":8,"target":1},{"source":1,"target":8},{"source":17,"target":13},{"source":8,"target":13},{"source":8,"target":3},{"source":13,"target":3},{"source":3,"target":2},{"source":39,"target":13},{"source":3,"target":39},{"source":15,"target":1},{"source":1,"target":15},{"source":3,"target":1},{"source":3,"target":8},{"source":3,"target":13},{"source":21,"target":3},{"source":13,"target":21},{"source":13,"target":39},{"source":21,"target":13},{"source":21,"target":27},{"source":27,"target":13},{"source":1,"target":3},{"source":1,"target":39},{"source":1,"target":2},{"source":1,"target":13},{"source":1,"target":21},{"source":1,"target":27}])];
所以我想我应该把 html_safe 放在那里..这给了
var links = [([{"source":2,"target":3},{"source":2,"target":1},{"source":8,"target":1},{"source":1,"target":8},{"source":17,"target":13},{"source":8,"target":13},{"source":8,"target":3},{"source":13,"target":3},{"source":3,"target":2},{"source":39,"target":13},{"source":3,"target":39},{"source":15,"target":1},{"source":1,"target":15},{"source":3,"target":1},{"source":3,"target":8},{"source":3,"target":13},{"source":21,"target":3},{"source":13,"target":21},{"source":13,"target":39},{"source":21,"target":13},{"source":21,"target":27},{"source":27,"target":13},{"source":1,"target":3},{"source":1,"target":39},{"source":1,"target":2},{"source":1,"target":13},{"source":1,"target":21},{"source":1,"target":27}])];
哪个更好,但是每个键都是一个字符串..我需要它不是字符串
我怎样才能使这项工作?是控制器的问题还是视图的问题?