不要在 ruby​​ on rails 中转义 html [英] Don't escape html in ruby on rails

查看:35
本文介绍了不要在 ruby​​ on rails 中转义 html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

rails 3 似乎逃避了一切,包括 html.我试过使用 raw() 但它仍然会转义 html.有解决方法吗?这是我正在使用的助手 (/helpers/application_helper.rb):

rails 3 seems to escape everything, including html. I have tried using raw() but it still escapes html. Is there a workaround? This is my helper that I am using (/helpers/application_helper.rb):

module ApplicationHelper
  def good_time(status = true)
    res = ""
    if status == true
      res << "Status is true, with a long message attached..."
    else
      res << "Status is false, with another long message"
    end
  end
end

我正在使用此代码在我的视图中调用助手:

I am calling the helper in my view using this code:

<%= raw(good_time(true)) %>

推荐答案

你可以像这样使用 .html_safe:

def good_time(status = true)
  if status
    "Status is true, with a long message attached...".html_safe
  else
    "Status is false, with another long message".html_safe
  end
end

<%= good_time(true) %>

这篇关于不要在 ruby​​ on rails 中转义 html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆