# Form.collection\_select html escaping data from database

**URL:** <https://www.ruby-forum.com/t/form-collection-select-html-escaping-data-from-database/193903>\
**Category:** Rails I18n\
**Created:** [August 26, 2010, 12:32pm UTC](https://www.ruby-forum.com/t/form-collection-select-html-escaping-data-from-database/193903 "2010-08-26T12:32:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [August 26, 2010, 12:32pm UTC](https://www.ruby-forum.com/t/form-collection-select-html-escaping-data-from-database/193903/1 "2010-08-26T12:32:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [August 26, 2010, 1:15pm UTC](https://www.ruby-forum.com/t/form-collection-select-html-escaping-data-from-database/193903/2 "2010-08-26T13:15:21Z")

</div>

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](http://www.google.com/search?q=ruby+decode+html+entities)

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [August 27, 2010, 6:46am UTC](https://www.ruby-forum.com/t/form-collection-select-html-escaping-data-from-database/193903/3 "2010-08-27T06:46:59Z")

</div>

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:  
> [Google Search](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?

---

<div class="post-metadata">

**Author:** ![mitch\_newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ea5d25/32.png) [@mitch\_newbie](https://www.ruby-forum.com/u/mitch_newbie)\
**Post date:** [August 30, 2010, 11:18am UTC](https://www.ruby-forum.com/t/form-collection-select-html-escaping-data-from-database/193903/4 "2010-08-30T11:18:20Z")

</div>

Mitchell G. 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:  
> > [Google Search](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(‘&’, ‘&’) %\>

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
