如何在不将数组转换为字符串的情况下在 js.erb 中使用控制器的数组?
How to use a controller's array in js.erb without converting array to a string?
home_controller.rb
class HomeController < ApplicationController
def index
@labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'].to_json.html_safe
end
end
_main.js.erb
window.onload = function() {
var ctx = document.getElementById('fgm').getContext('2d');
console.log("<%= escape_javascript @labels %>");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: "<%= escape_javascript @labels %>",
浏览器控制台日志
["Red","Blue","Yellow","Green","Purple","Orange"]
浏览器调试
<script type="text/javascript">
window.onload = function() {
var ctx = document.getElementById('fgm').getContext('2d');
console.log("[\"Red\",\"Blue\",\"Yellow\",\"Green\",\"Purple\",\"Orange\"]");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: "[\"Red\",\"Blue\",\"Yellow\",\"Green\",\"Purple\",\"Orange\"]",
datasets: [{
label: '# of Votes',
这一行是我的问题:
labels: "[\"Red\",\"Blue\",\"Yellow\",\"Green\",\"Purple\",\"Orange\"]",
因为我需要它不要用引号引起来,例如
labels: ["Red","Blue","Yellow","Green","Purple","Orange"],
使用这个:
@labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'].to_json.html_safe
并像这样使用它,没有周围的 "
并且你不需要转义它(它已经用 to_json 和 html_safe 转义了):
console.log(<%= @labels %>);
labels: <%= @labels %>,
下面的代码应该可以工作
home_controller.rb
class HomeController < ApplicationController
def index
@labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange']
# You don't need to use to_json and html_safe
end
end
_main.js.erb
window.onload = function() {
var ctx = document.getElementById('fgm').getContext('2d');
labels = <%= raw @labels %> # use raw method
console.log(labels);
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,
home_controller.rb
class HomeController < ApplicationController
def index
@labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'].to_json.html_safe
end
end
_main.js.erb
window.onload = function() {
var ctx = document.getElementById('fgm').getContext('2d');
console.log("<%= escape_javascript @labels %>");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: "<%= escape_javascript @labels %>",
浏览器控制台日志
["Red","Blue","Yellow","Green","Purple","Orange"]
浏览器调试
<script type="text/javascript">
window.onload = function() {
var ctx = document.getElementById('fgm').getContext('2d');
console.log("[\"Red\",\"Blue\",\"Yellow\",\"Green\",\"Purple\",\"Orange\"]");
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: "[\"Red\",\"Blue\",\"Yellow\",\"Green\",\"Purple\",\"Orange\"]",
datasets: [{
label: '# of Votes',
这一行是我的问题:
labels: "[\"Red\",\"Blue\",\"Yellow\",\"Green\",\"Purple\",\"Orange\"]",
因为我需要它不要用引号引起来,例如
labels: ["Red","Blue","Yellow","Green","Purple","Orange"],
使用这个:
@labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'].to_json.html_safe
并像这样使用它,没有周围的 "
并且你不需要转义它(它已经用 to_json 和 html_safe 转义了):
console.log(<%= @labels %>);
labels: <%= @labels %>,
下面的代码应该可以工作
home_controller.rb
class HomeController < ApplicationController
def index
@labels = ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange']
# You don't need to use to_json and html_safe
end
end
_main.js.erb
window.onload = function() {
var ctx = document.getElementById('fgm').getContext('2d');
labels = <%= raw @labels %> # use raw method
console.log(labels);
var myChart = new Chart(ctx, {
type: 'line',
data: {
labels: labels,