【问题标题】:html_safe in ruby on rails not workingruby on rails 中的 html_safe 不起作用
【发布时间】:2014-05-19 05:23:34
【问题描述】:

我正在使用 ruby​​ 2.0.0 这是我的控制器..

@mail_msg = @store_items.where(id: params[:button_id]).first.email_confirmation_text
p "-------------------------"
p @mail msg
p @mail_msg.html_safe

这是我的控制台(终端)输出

"-------------------------"
"<p>You have purchased Spice It Up. Points have been redeemed from your main account.</p>"
"<p>You have purchased Spice It Up. Points have been redeemed from your main account.</p>"

我在控制台中得到的是相同的。我无法逃脱 html 标签。

更新

我认为我有这个价值.. 在我的视图页面中

<%= @mail_msg.html_safe %>

还是不行..

请帮忙

【问题讨论】:

  • 试试看它会起作用
  • 应该在使用 html 的视图中工作。你在控制器中使用html?还没看过
  • 是的,因为 html_safe 是一个辅助方法。
  • 如果您在控制器中使用 html_safe 作为@mail_msg.html_safe,那么在视图中只需将其作为&lt;%= @mail_msg %&gt;。它会起作用。
  • 您误解了html_safe 的用途。它将字符串标记为“可以安全地包含在 HTML 中”,这样模板系统就不会对其进行 HTML 编码。

标签: ruby-on-rails ruby ruby-on-rails-4 html-safe


【解决方案1】:

试试这些可能会对你有帮助

strip_tags("Strip <i>these</i> tags!")

# => 去掉这些标签!

strip_tags("<b>Bold</b> no more!  <a href='more.html'>See more here</a>...")

# => 不再粗体!在这里查看更多...

strip_tags("<div id='top-bar'>Welcome to my website!</div>")

# => 欢迎来到我的网站!enter code here

【讨论】:

【解决方案2】:

html_saferaw 在视图中工作,使用

<%= raw @mail_msg %> 

<%= @mail_msg.html_safe %>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 2012-04-17
    • 2015-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多