小编rob*_*nex的帖子

render:json =>'string here'预期结果

我已经完成了这一千次,但我仍然不熟悉render :json手柄弦如何.

要设置范围,我们来谈谈Rails 3

这就是它现在的行为方式:

...
render :json => 'This is the string'
...
Run Code Online (Sandbox Code Playgroud)

将返回浏览器:

This is the string
Run Code Online (Sandbox Code Playgroud)

这实际上不是有效的JSON响应:S

理想情况下它应该呈现这样的东西:

"This is the string"
Run Code Online (Sandbox Code Playgroud)

铁轨指南甚至说:

您不需要在要渲染的对象上调用to_json.如果使用:json选项,render将自动为您调用to_json.

并且调用"This is the string".to_json实际上正在"\"This is the string\""按预期返回.

"This is the string".to_json #=> "\"This is the string\""
Run Code Online (Sandbox Code Playgroud)

我错了吗?

json ruby-on-rails

34
推荐指数
1
解决办法
2万
查看次数

PNG IDAT 规范

我正在阅读 W3 PNG 规范(从头开始创建一个 PNG 库),我终于找到了如何创建绿色 1x1 图像。

现在我正在尝试创建一个更大的混合红色、绿色和蓝色像素的图像。假设一个 4x4 图像。可悲的是,我将所有像素混合在一起,其中一些是黑色或粉红色。

细节:

  • 签名:好的
  • IHDR:好的
    • 宽度:4
    • 高度:4
    • 位深:8
    • 颜色:2
    • 过滤器:0
    • 压缩:0
    • 隔行:0
  • IDATA:4 块*
  • IEND:好的

*IDATA 块:

  • 4字节长度:Zlib deflate后DATA的字节数
  • 4*1 字节类型:IDAT
  • X 字节数据:4*4bytes 无符号整数,Zlib 压缩,一个接一个,网络字节序
    • 255 蓝色**
    • 65280 绿色**
    • 16711680 为红色**
  • 4 字节 crc:好的

**按位结果:

alpha<<24 | red<<16 | green<<8 | blue
Run Code Online (Sandbox Code Playgroud)

alpha、蓝色、绿色和红色取值从 0 到 255

这有什么问题?

png encode image image-processing

3
推荐指数
1
解决办法
1923
查看次数

凌乱&从Rails 3控制器返回查看

在我的Rails 3 app控制器上,我有以下代码:

array = []
Location.all.each{|x|array<<x.city.html_safe}
@data_dump = array
Run Code Online (Sandbox Code Playgroud)

在Rails控制台中,它看起来很干净:

["Littelside", "Tessmouth"]
Run Code Online (Sandbox Code Playgroud)

在我看来,@ data_dump对象被编码:

[&quot;Littelside&quot;, &quot;Tessmouth&quot;]
Run Code Online (Sandbox Code Playgroud)

你怎么清理这个烂摊子?我希望我的对象在视图中,以对象在终端中返回.提前致谢!

arrays quotes ruby-on-rails html-safe ruby-on-rails-3

2
推荐指数
1
解决办法
1961
查看次数