【问题标题】:Using .html_safe in an each loop在每个循环中使用 .html_safe
【发布时间】:2014-12-29 21:39:02
【问题描述】:

我在.each 循环中使用.html_safe 时遇到了问题。这是我可以设置的方法...

class Players < ApplicationController::Base
  def index
    @players = Players.all
  end
end

在视图中:

<% @players.each do |player| %>
  <div>
    <%= player.stat_code.html_safe %>
  </div>
<% end %>

现在stat_code 将是一段很长的 HTML 代码块......(我没有这个案例的示例)但它是 HTML。

.each 循环中使用.html_safe 似乎没有任何作用。为什么什么都没有发生,您会推荐什么其他解决方案?

注意:我还读到使用.html_safe 很糟糕!对于这种特殊情况,我不在乎。

【问题讨论】:

  • 我想我用sanitize而不是html_safe完成了我的问题....

标签: html ruby-on-rails erb


【解决方案1】:

我设法通过使用sanitize 而不是.html_safe 设置视图来解决我的问题...

<% @players.each do |player| %>
  <div>
    <%= sanitize player.stat_code %>
  </div>
<% end %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-08
    • 2013-11-25
    • 2015-10-06
    • 1970-01-01
    • 2014-08-02
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多