'Keep &nbsp entities in rails sanitize

Using either the rails sanitize method, or any gems (such as the sanitize gem), it is possible to keep certain tags, but also keep certain html elements such as &nbsp.

So far I have only found options for keeping tags and attributes, but not elements.

Thanks, Owen



Solution 1:[1]

You can use entity number instead of entity name for allowed tags option.

For the   - entity number is  

if a variable has value Hello <br>User</br>, how&#160;old&#160;are&#160;you? , in a rails view it could look

sanitize(text_with_html, tags: %w(br &#160;))

It's checked for rails 6.1, but I suppose it works for previous versions also

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 s.stv