【问题标题】:Output HTML in a RJ ERB template在 RJ ERB 模板中输出 HTML
【发布时间】:2011-08-26 12:20:44
【问题描述】:

这可能真的很愚蠢,但我根本无法让它工作......

我有vacation.js.erb,它会在ajax 调用完成时生成一些javascript 来更新页面。文件源如下所示:

$('semantic').replace('<div id="semantic"></div>');
<%
    html = '<table class="ajax">'
    html += "<tr><td>EU - 27</td><td>#{@eu.value.value} days</td></tr>"
    html += "<tr><td>#{@source.location.value}</td><td>#{@source.value.value } days></td></tr>" unless @source.location.value.blank?
    html += "<tr><td>#{@cv.country.name}</td><td>No Data Available</td></tr>" if @source.location.value.blank?
    html += "<tr><td>#{@target.location.value}</td><td>#{@target.value.value } days></td></tr>" unless @target.location.value.blank?
    html += "<tr><td>#{@vacancy.country.name}</td><td>#{@target.value.value } days></td></tr>" if @target.location.value.blank?
    html += "</table>"
%>
$('semantic').insert('<%= escape_javascript(html) %>');

我可以用浏览器清楚地浏览代码,但最后一行给我带来了麻烦。它将html中的内容编码为html实体,我不想要这个,因为它破坏了javascript。 Fiddler 中的响应如下所示:

$('语义').replace(''); $('语义').insert('

欧盟 - 27 28 天 gb 没有 可用数据 美联航 王国 29 天> ');

如何防止页面编码 html 变量? 我尝试使用 但没有返回任何内容。

【问题讨论】:

    标签: javascript ruby-on-rails ajax prototypejs


    【解决方案1】:

    修复它,解决方案是

    $('semantic').replace('<div id="semantic"></div>');
    <%
        html = '<table class="ajax">'
        html += "<tr><td>EU - 27</td><td>#{@eu.value.value} days</td></tr>"
        html += "<tr><td>#{@source.location.value}</td><td>#{@source.value.value } days></td></tr>" unless @source.location.value.blank?
        html += "<tr><td>#{@cv.country.name}</td><td>No Data Available</td></tr>" if @source.location.value.blank?
        html += "<tr><td>#{@target.location.value}</td><td>#{@target.value.value } days></td></tr>" unless @target.location.value.blank?
        html += "<tr><td>#{@vacancy.country.name}</td><td>#{@target.value.value } days</td></tr>" if @target.location.value.blank?
        html += "</table>"
    %>
    $('semantic').insert('<%=raw html  -%>');
    

    在这里找到解决方案:Disable HTML escaping in erb templates

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多