简体   繁体   English

如何在Rails 3.2中从助手输出上标2?

[英]How do I output a superscript 2 from a helper in Rails 3.2?

I would like to output a string from a helper like '30 Meters2' (but with a superscript 2). 我想从类似“ 30 Meters2”的助手中输出一个字符串(但带有上标2)。

The HTML entity for a superscript 2 is ² so I thought something like this would work: 上标2的HTML实体为²,所以我认为这样可以工作:

"30 Meters " + raw("²")

But it doesn't work. 但这是行不通的。

How can I do this? 我怎样才能做到这一点?

Here's the entire method: 这是整个方法:

  def area_conversion(feet, project)
    if project.metric
      "#{(feet * 0.0929).round} Meters" + raw("²")
    else
      "#{feet} sq. ft. "
    end
  end

Using html_safe doens't seem to work either: 使用html_safe似乎也不起作用:

  def area_conversion(feet, project)
    if project.metric
      "#{(feet * 0.0929).round} Meters" + "²".html_safe
    else
      "#{feet} sq. ft. "
    end
  end

I think you need to declare the whole string as html_safe because a safe string merged with a unsafe string gets unsafe again. 我认为您需要将整个字符串声明为html_safe因为安全字符串与不安全字符串合并会再次变得不安全。 In your case it should be save because a string multiplied by a float is empty, so nobody can not put dangerous code into your string here. 在您的情况下,应将其保存,因为字符串乘以浮点数是空的,因此没有人不能在此处将危险代码放入您的字符串中。 So this: 所以这:

"#{(feet * 0.0929).round} Meters²".html_safe

should be fine. 应该没事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM