Questions tagged [html-safe]
75 questions
26
votes
7 answers
How to remove HTML markup from string
Let's say I have:
@string = "it is a string"
I want to use it in different parts of my application in two ways:
With a clickable link
Without the clickable link (but not showing any HTML markup)
The first one can be done using…

gabrielhilal
- 10,660
- 6
- 54
- 81
19
votes
1 answer
Ruby on Rails: difference between .html_safe and sanitize()
I have two pieces of code in my view:
<%= sanitize('
winter
') %> <%= 'winter
'.html_safe %> And they both seem to result in encoding html tags in a string provided. What is the difference between them and when should I use either?
Evgenia Karunus
- 10,715
- 5
- 56
- 70
12
votes
3 answers
Rendering partial in bootstrap popover rails 5 app?
I'm having a problem rendering a partial in a bootstrap popover in my rails app.
The partial is always rendered as a plain text( showing all the HTML tags etc).
this is the code from the index.html.erb

codegirl
- 377
- 1
- 5
- 18
12
votes
2 answers
Using simple_format and html_safe at the same time in Rails
In the @post.content, I want
1.simple_format, so content would have different lines rather than in a single line without breaks
2.html_safe, so user could paste some

ZK Zhao
- 19,885
- 47
- 132
- 206
12
votes
3 answers
Forcing HTML Escaping in Rails 3
I'm running into an issue with the rails auto-escaping. It currently thinks a string is html_safe (which it is), but for display purposes I need it to still escape the html. Here's the steps the string is taking.
my_string = render(:partial =>…

Jason Logsdon
- 507
- 5
- 19
10
votes
4 answers
Rails 3: Using HTML in i18n form helper translations
I am using the automatic form label helper for creating my form labels and having them translated via the i18n support, however, I want to have HTML in the label and I can't figure out how to make it HTML safe.
For example:
en:
helpers:
…

Jeremy Baker
- 3,986
- 3
- 24
- 27
9
votes
2 answers
Why in Rails 3, <%= note.html_safe %> and <%= h note.html_safe %> give the same result?
It feels like html_safe adds an abstraction to the String class that requires understanding of what is going on, for example,
<%= '1 2' %> # gives 1 <b>2</b> in the HTML source code
<%= h '1 2' %> # exactly the…

nonopolarity
- 146,324
- 131
- 460
- 740
7
votes
1 answer
Rails 4 raw html_safe not working
I am not able to escape raw html tags in the view
<% array = @article.tags.collect { |p| (link_to p.name, '#') } %>
<%= array.join(' , ') %>
Raw and html_safe
<% array = @article.tags.collect { |p| raw (link_to p.name, '#') } %>
<% array = …

Ashwin Yaprala
- 2,737
- 2
- 24
- 56
6
votes
3 answers
How to make rails 3 I18n translation automatically safe?
I use rails 3. Is there any easy way to tell I18n to respect 'html safness' of string used in interpolation and make all translated string html safe by default? So if I have this en.yml:
en:
user_with_name: 'User with name %{name}'
and I…

tig
- 25,841
- 10
- 64
- 96
5
votes
1 answer
Is safe for sanitize?
I am using a rich text editor (CKEditor) and I have the opportunity to let users create profiles that are displayed to other users.
Many of the attributes CKEditor can control are being lost when I display them as:
<%= sanitize(profile.body) %>
My…

sscirrus
- 55,407
- 41
- 135
- 228
5
votes
4 answers
Parsing newline characters in textareas without allowing all html tags
I have a textarea field where users can enter content. When it comes to displaying their entry on a page, rails returns \n for each line break, which appears as no break at all for html on the page.
From what I gather, the standard way of getting…

PlankTon
- 12,443
- 16
- 84
- 153
4
votes
2 answers
Rails3 CSV putting "" instead of actual quotes
Similar to this question except I don't use html_safe anywhere in the whole project.
I generate a CSV file in index.csv.erb like this:
<%=
response.content_type = 'application/octet-stream'
CSV.generate do |csv|
@persons.each do |person|
csv…

Nicolas Raoul
- 58,567
- 58
- 222
- 373
4
votes
3 answers
Rails 3 Submit Tag + html_safe
What's wrong with this line of code?
<%= submit_tag "Delete ".html_safe, :disable_with => "Deleting", :class => "btn btn-danger"%>
This literally produces:
Evidently my html_safe call isn't doing…

Noz
- 6,216
- 3
- 47
- 82
4
votes
1 answer
Convert Ruby string with ampersand-hash-char-semicolon characters into an ascii or html friendly string
Using Rails 3 I am consuming an XML feed generated in drupal or something. The tags it gives me look like:
So the intention is that this should really look…

genkilabs
- 2,966
- 30
- 36
3
votes
2 answers
Ruby/Rails - Is there an easy way to make hard-coded HTML symbols html_safe?
In my view I want to display some right double angle quotes in my link.
Before Rails 3, this worked:
<%= link_to "» #{@category.name}", some_path %>
Now what should I do if I want to specify the » as html_safe but not the rest of the…

johnnycakes
- 2,440
- 2
- 28
- 36