Forum: Rails I18n form.collection_select html escaping data from database

Posted by Mitchell Gould (mitch_newbie)
on 2010-08-26 12:32
I have french unicoded coded characters in my database.  They use the
ê format.

When I use form.collection_select it turns the & into & and then the
french character is not displayed.

How can I resolve this.


Thanks in advance
Posted by Henrik --- (malesca)
on 2010-08-26 13:15
(Received via mailing list)
I wouldn't use HTML entities in the database since you may display
them in other contexts than HTML/XML, and also get problems like this
one. So if possible, store the text in some unicode encoding in the
database, without entities.

If you must use entities for some reason, I suppose you could just
decode them before passing to collection_select. I think
CGI.unescapeHTML is pretty limited, but there are libs:
http://www.google.com/search?q=ruby+decode+html+entities
Posted by Mitchell Gould (mitch_newbie)
on 2010-08-27 06:46
Henrik --- wrote:
> I wouldn't use HTML entities in the database since you may display
> them in other contexts than HTML/XML, and also get problems like this
> one. So if possible, store the text in some unicode encoding in the
> database, without entities.
> 
> If you must use entities for some reason, I suppose you could just
> decode them before passing to collection_select. I think
> CGI.unescapeHTML is pretty limited, but there are libs:
> http://www.google.com/search?q=ruby+decode+html+entities

The problem no matter what I am using as encoding the form helper 
function form.collection_select changes the & to �.

If I put the same code for the french accent on the page I get the 
correct output.

Norvège => Norvège.

I really just want to turn the html escaping off so that the characters 
are displayed properly.

That would solve one of my problems.  Why does rails make it so hard to 
do basic stuff with language?
Posted by Mitchell Gould (mitch_newbie)
on 2010-08-30 11:18
Mitchell Gould wrote:
> Henrik --- wrote:
>> I wouldn't use HTML entities in the database since you may display
>> them in other contexts than HTML/XML, and also get problems like this
>> one. So if possible, store the text in some unicode encoding in the
>> database, without entities.
>> 
>> If you must use entities for some reason, I suppose you could just
>> decode them before passing to collection_select. I think
>> CGI.unescapeHTML is pretty limited, but there are libs:
>> http://www.google.com/search?q=ruby+decode+html+entities
> 
> The problem no matter what I am using as encoding the form helper 
> function form.collection_select changes the & to �.
> 
> If I put the same code for the french accent on the page I get the 
> correct output.
> 
> Norvège => Norvège.
> 
> I really just want to turn the html escaping off so that the characters 
> are displayed properly.
> 
> That would solve one of my problems.  Why does rails make it so hard to 
> do basic stuff with language?

I found a way around the problem. I am not sure if its the best 
practices way but here it is

      <%= form.collection_select(  :country, @countries, :name, :name, 
options ={ :prompt => t('services.req_form.country_prompt')} 
).gsub('&amp;', '&')  %>

collection_select escapes the & used for my french character html 
entity. So I replace all instances of the &amp with &. It works.

I would appreciate any comments if this is ok to do and if not what is a 
better way.

Thanks
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.