【发布时间】:2018-09-03 11:25:31
【问题描述】:
在 Rails 5 中,我想在 Javascript 中使用 Rails 数组的内容:
在我的 harddisk_controller.rb 中:
@locations = Harddisk.select(:location).distinct # grab a collection of distinct harddisk locations objects
@harddisk_locations = []
@locations.each do |location|
@harddisk_locations << location.location # put only the location names into an array
end
我正在尝试实现将 Rails 的 @harddisk_locations 的内容加载到 Javascript 的 harddisk_locations 中:
在 application.js 中:
var harddisk_locations = [<%= raw @harddisk_locations.to_json %>];
但是我在浏览器控制台的 [] 上收到错误消息:
Uncaught SyntaxError: Unexpected token
我认为 Javascript 是在抱怨
紧接着
[
字符。如何解决这个问题?
【问题讨论】:
-
查看源代码并查看
var harddisk_locations = [<%= raw @harddisk_locations.to_json %>];呈现的内容