新手问题 model 返回数组问题

xiaoxiao · 2013年07月13日 · 最后由 Rei 回复于 2013年07月13日 · 3162 次阅读

我在我的 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

需要 登录 后方可回复, 如果你还没有账号请 注册新账号