在渲染表达式的开头和结尾处转义引号

时间:2013-10-30 13:52:43

标签: jquery ruby-on-rails

我正在使用这样的代码为jquery提供rails params:

 <% @num_slides.times do |n| %> 
         parent.$("#data-store").data("background[<%=n%>]","<%= @background[n.to_s] %>" );
         parent.$("#data-store").data("content[<%=n%>]", '<%= @content[n.to_s].squish %>');
  <% end %>

它适用于后台,但它会呈现如下内容:

content[0]: "&amp;lt;div class=&quot;ui-wrapper ui-draggable&quot; style=&quot;overflow: hidden; position: absolute; width: 128px; height: 128px; top: 223.1111125946045px; left: 243.11111450195313px; margin: 0px;&quot;&amp;gt;&amp;lt;img id=&quot;link1&quot; style=&quot;position: static; margin: 0px; resize: none; zoom: 1; display: block; height: 128px; width: 128px;&quot; src=&quot;http://i.imgur.com/cou2Yxj.png &quot; class=&quot;ui-resizable&quot;&amp;gt;&amp;lt;div class=&quot;ui-resizable-handle ui-resizable-e&quot; style=&quot;z-index: 90;&quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div class=&quot;ui-resizable-handle ui-resizable-s&quot; style=&quot;z-index: 90;&quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;div class=&quot;ui-resizable-handle ui-resizable-se ui-icon ui-icon-gripsmall-diagonal-se&quot; style=&quot;z-index: 90;&quot;&amp;gt;&amp;lt;/div&amp;gt;&amp;lt;img class=&quot;icon-layer-up icon-on-img&quot; src=&quot;/assets/icon_layer_up.png&quot; style=&quot;z-index: 2;&quot;&amp;gt;&amp;lt;img class=&quot;icon-layer-down icon-on-img&quot; src=&quot;/assets/icon_layer_down.png&quot; style=&quot;z-index: 2;&quot;&amp;gt;&amp;lt;img class=&quot;icon-trash1 icon-on-img&quot; src=&quot;/assets/icon_trash.png&quot; style=&quot;z-index: 2;&quot;&amp;gt;&amp;lt;img class=&quot;icon-copy-el icon-on-img&quot; src=&quot;/assets/icon_copy.png&quot; style=&quot;z-index: 2;&quot;&amp;gt;&amp;lt;img class=&quot;icon-layer-up icon-on-img&quot; src=&quot;/assets/icon_layer_up.png&quot; style=&quot;z-index: 2;&quot;&amp;gt;&amp;lt;img class=&quot;icon-layer-down icon-on-img&quot; src=&quot;/assets/icon_layer_down.png&quot; style=&quot;z-index: 2;&quot;&amp;gt;&amp;lt;img class=&quot;icon-trash1 icon-on-img&quot; src=&quot;/assets/icon_trash.png&quot; style=&quot;z-index: 2;&quot;&amp;gt;&amp;lt;img class=&quot;icon-copy-el icon-on-img&quot; src=&quot;/assets/icon_copy.png&quot; style=&quot;z-index: 2;&quot;&amp;gt;&amp;lt;/div&amp;gt;"

所以我在开头和结尾都有双引号,当我使用这样的代码把它放到html中时我得到行HTML代码作为幻灯片的内容(它们被视为文本):

 for (var i = 0; i < slides.length; i++) {
          slides[i].innerHTML = contentObject["content[" + i + "]"];
          slides[i].style.backgroundImage = contentObject["background[" + i + "]"];
      }

我怎么能逃避这个引用?

1 个答案:

答案 0 :(得分:1)

html_safe是您需要在此处添加的内容:

parent.$("#data-store").data("content[<%=n%>]", '<%= @content[n.to_s].squish.html_safe %>');