简体   繁体   中英

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).

The HTML entity for a superscript 2 is ² so I thought something like this would work:

"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:

  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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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