【问题标题】:Rails' render is generating bizarre, subtly wrong contentRails 的渲染正在生成奇怪的、微妙的错误内容
【发布时间】:2011-08-04 23:22:39
【问题描述】:

我有一个为人们托管简单 html 页面的应用程序。其中一些人使用表格进行设计布局。 (不是我,我发誓!)出于某种神秘的原因,渲染会破坏 Internet Explorer 中的这些布局。

这是我能够重现该问题的最简单方法。

当我将 html 文件保存到 public/ 并通过 Mongrel 访问它时,它在所有浏览器中看起来都很好。如果我使用以下代码来呈现文件,它会变得很时髦:

render :file => '[app_directory]/public/sample.html'

Internet Explorer 会为每个换行符创建一个空文本节点,这会导致表格出现空白。原始 html 显然是相同的,但是当它与渲染一起提供时,它会以某种方式改变,让 Internet Explorer 不满意。

对于如何阻止这种情况的任何指示,我将非常感激。谢谢!

编辑 1:谜团加深...

我已经弄清楚如何让 Chrome 出现同样的错误。这是在 Chrome 中工作的代码,在 IE 中被破坏:

# tab_controller.rb
render :inline => tab.public_html

但是当我将其更改为使用外部 ERB 模板时,奇怪的空白也会出现在 Chrome 中:

# tab_controller.rb
@html = tab.public_html
render 'show'

# show.erb
<%=raw @html %>

我确信对此有一个解释,但我越挖越陌生。

编辑 2:标题

以下是 IE 为静态服务文件提供的响应标头:

Response        HTTP/1.1 200 OK
Connection      close
Date            Fri, 05 Aug 2011 00:33:20 GMT
Last-Modified   Thu, 04 Aug 2011 23:38:21 GMT
Content-Type    text/html
Content-Length  913

以下是 IE 为通过渲染生成的内容提供的响应标头。有问题的内容:

Response         HTTP/1.1 200 OK
Connection       close
Date             Fri, 05 Aug 2011 00:28:18 GMT
Content-Type     Text/html
X-UA-Compatible  IE=Edge
ETag             "43d392ddbbcf3856ced3de672005c26f"
Cache-Control    max-age=0, private, must-revalidate
Set-Cookie       _rails_static_html_session=[session stuff]; path=/; HttpOnly
X-Runtime        31.964569
Transfer-Encoding   chunked

编辑 3:代码

您可以(我希望)通过将以下代码放入任何随机控制器来重现该问题:

html = "<html>\n<head>\n</head>\n\n<body>\n<table width='520' height='870' border='0' cellpadding='0' cellspacing='0'>\n  <tr>\n    <td align='center' valign='top'><div align='center'>\n      <table width='520' border='0' cellspacing='0' cellpadding='0'>\n        <tr>\n          <td><img src='.' width='520' height='314' /></td>\n        </tr>\n        <tr>\n          <td><img src='.' width='520' height='320' /></td>\n        </tr>\n        <tr>\n          <td><img src='.' width='520' height='166' /></td>\n        </tr>\n        <tr>\n          </tr>\n      </table>\n    </div></td>\n  </tr>\n</table>\n</body>\n</html>"

render :inline => html
File.open('/railsappdirectory/public/sample_html.html', 'w') {|f| f.write(html) }

当您通过 Rails 应用程序使用 Internet Explorer 访问此内容时,图像之间会有间隙。当您将html文件视为静态资产时,不会有任何间隙。

【问题讨论】:

  • 您是否将 Explorer 用于公共视图以及何时使用 render? IE 呈现内容的方式与其他浏览器不同。
  • 是的。哈哈,我敏锐地(痛苦地)意识到 IE 呈现内容的方式与其他浏览器不同。我喜欢你这么说。 :)
  • 这可能与内容标题有关。如果使用 render_to_string 获取原始文本会发生什么?
  • 詹姆斯,我认为你对标题的看法可能是对的。我刚刚尝试了 render_to_string,它似乎是绝对正常的标记。我将编辑问题以添加有关出现问题和不出现问题的标题的信息。
  • 您应该将您的解决方案添加为答案并接受它,以便将此问题标记为已回答。有趣的问题,感谢您发布解决方案。

标签: ruby-on-rails internet-explorer actionview


【解决方案1】:

我使用一个简单的 php 文件来提供具有不同标题的相同内容,发现使用以下标题栏导致了问题:

X-UA-Compatible: IE=Edge

this question 上的评论解释了如何阻止 rails 服务于这个标题。在您的 config/environments/*.rb 文件中,确保以下设置为 false:

config.action_dispatch.best_standards_support = false

感谢大家的帮助!

【讨论】:

    猜你喜欢
    • 2015-09-05
    • 1970-01-01
    • 1970-01-01
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多