2

I have <%= f.label :cost, "Cost (£/m)" %> in a form. When the form is rendered the "£" sign comes out as �. I am using utf-8 encoding. Why is this happening?

freshest
  • 6,229
  • 9
  • 35
  • 38

3 Answers3

3

Suggestion from my experience) :

  1. verify that your file erb is saved in utf-8
  2. I recomend you to use number_to_currency method
Sergey
  • 785
  • 1
  • 5
  • 14
  • How do I verify if the erb is being saved in utf-8. I am not outputting a number just the £ symbol. – freshest Nov 08 '10 at 20:47
  • When you save the file in your text editor, check the UTF-8 encoding is selected. Many Windows text editors will save under the non-UTF-8 ‘system code page’ (also—misleadingly—known as ANSI) by default, which is unfortunate. – bobince Nov 08 '10 at 20:52
  • A set of editors has got View->Show current encoding functionality, for example, personally, I like PSPad. – Sergey Nov 08 '10 at 20:54
  • Using Dreamweaver as editor, I have escaped to £ in other erb with success, but this form partial just doesn't work. – freshest Nov 08 '10 at 21:03
  • @freshest, what form partial you mentioned? – Sergey Nov 08 '10 at 21:24
  • My code `<%= f.label :cost, "Cost (£/m)" %>` is in a form which is in a Ruby on Rails partial _form.html.erb which is used in views new.html.erb and edit.html.erb. – freshest Nov 08 '10 at 23:37
  • 1
    You can use a Ruby string literal escape. What that looks like depends on what encoding you're serving the page as. In ISO-8859-1, `"Cost (\xA3/m)"`. In UTF-8, `"Cost (\xC2\xA3/m)"`. Really though you should be serving your pages, and saving your files, as UTF-8, in which case you should just be able to use `£` directly. [Dreamweaver settings.](http://superuser.com/questions/46693/dreamweaver-reverts-to-western-encoding-after-its-been-changed-to-utf8/48723#48723) – bobince Nov 09 '10 at 12:39
  • @bobince - After some more investigation I have discovered that all the files RoR generates when creating a new application seem to be saved with Western(ISO-8859-1) encoding (I am developing on a Windows machine). And so I am having this confict with the £ character. Should RoR be generating the framework files in this way? If not how can I set it to generate the files with UTF-8 encoding? – freshest Nov 10 '10 at 23:27
1

Are you outputting HTML? If so you need to escape the character <%= f.label :cost, "Cost (&pound;/m)" %>

Sean Kelleher
  • 1,952
  • 1
  • 23
  • 34
  • I am using `` and I am outputing HTML. I have also tried escaping to £ , when I do this I just get £. The form is created in a partial, if that is relevant. – freshest Nov 08 '10 at 20:42
0

Either send your page with UFT-8 encoding, or send the pound sign as &pound; (or &#163; if you're sending XML rather than HTML).

Spudley
  • 166,037
  • 39
  • 233
  • 307
  • @freshest - really? I assume you're seeing the encoded string as per your comment on the other answer? In that case, I'd say your problem is that the label function is trying to do its own encoding for you. How you solve that, I don't know. Sorry. – Spudley Nov 08 '10 at 20:55