ahmet
ahmet

Reputation: 5005

UTF-8 issue in Ruby on Rails with × character

<a class="close" href="#">×</a>

I get an error regarding the use of ×.

It's used in error messages on twitter's bootstrap framework, I get an invalid byte sequence in UTF-8 error when I try to use it. Is there any work-around? Apart from using a normal x or X.

I have:

# Configure the default encoding used in templates for Ruby 1.9.
    config.encoding = "utf-8"

In my application.rb

Upvotes: 2

Views: 681

Answers (4)

Tim Kretschmer
Tim Kretschmer

Reputation: 2280

#encoding: utf-8
class ClassiClass
end

everything works fine!

Upvotes: 1

Eden Townsend
Eden Townsend

Reputation: 395

What editor are you using?

I suspect that you are saving the source file using an encoding other than UTF-8 (such as Latin-1 or ANSI on Windows), which is then causing ruby to fail to interpret the file correctly.

I've tried adding the times symbol to one of my views (using HAML) and it worked correctly. I'm using VIM as my editor and saving in UTF-8 without any BOM.

Upvotes: 1

Peter Brown
Peter Brown

Reputation: 51707

You need to set the encoding at the top of the file where that character is used. You can do this with:

# coding: utf-8
class MyClass
end

I haven't tried it in an erb file, but I don't see why that would be any different. I think you can use the word "encoding" too instead of just "coding" if that feels better. All that is required is at minimum "coding".

Upvotes: 2

Preacher
Preacher

Reputation: 2420

This seems almost too simple, but why aren't you using &times;?

Upvotes: 2

Related Questions