新手问题 model 返回数组问题

xiaoxiao · July 13, 2013 · Last by Rei replied at July 13, 2013 · 3162 hits

我在我的 model 定义了一个方法如下: class Bom < ActiveRecord::Base def self.cal(fileid) arrs=[] arr=[] Bom.where(:fileid=>fileid).each do |m| if m.quantitym.code).first.quantity.to_i arr<m.code).first.quantity arr<<1 arrs<<arr end arr=[] end return arrs end end 现在需要这个返回值,如果在 rails c 上调用这个函数 Bom.cal(1) 返回一个数组如下 [["01010001", -2, "1"], ["01010002", -3, "1"]] 现在在浏览器上却出现错误 这怎么解决。。。。

这是把结果拼接到 javascript 了吗?

对,我在 js 中需要的是 [["01010001", -2, "1"], ["01010002", -3, "1"]] 这样的格式,后台返回的也是正确的,就是把引号处理成&quto 了,怎么将&quto 转换回引号

#2 楼 @xiaoxiao 可以参考下 j 这个 helper

全名叫 escape_javascript

html_safe or raw

thx 照你说的解决了,呵呵

#3 楼 @loveky 居然有这么个 helper,学习了

#6 楼 @blacktulip 我也是在书上看到的。。。

#4 楼 @blacktulip 不要轻易露出 html_safe 和 raw

You need to Sign in before reply, if you don't have an account, please Sign up first.