How to show string in xml format in erb file - ruby-on-rails

In the controller, I have an variable #xml_string = "<tag> hello \n world </tag>". Now I want to show the content of #xml_string. In erb file I wrote <%= #xml_string %>, but this can only display hello world, the xml tag <tag> </tag> was missed and \n was ignored.
Aslo , <% render :text => #xml_string , :content_type = 'application/xml' %> would not show any thing at all.
what is the correct way to achieve this? Thanks.

HTML ignores new line characters and white spaces unless you wrap the content into a tag that is whitespace-aware.
<pre><%=h #xml_string %></pre>
Otherwise, replace the "\n" with a line break. In this case you need to manually escape the HTML string.
<%=h #xml_string.gsub("<", "<").gsub("\n", "<br>") %>

try:
<%=h #xml_string %>

You could use this:
<%=h #xml_string.dump[1..-2] %>
The dump method will simply return the string in a way that makes str == eval(str.dump). That means it includes the quotes, so you need the [1..-2] to slice those away.

Related

line break for a string placeholder in ruby

<%= f.text_area :comment, placeholder: "Comment on your track or" + \n + "share your favorite lyrics" %>
How can I get the placebolder to line break like
Solution was just to add white space so the next line wraps:
placeholder: "Comment on your track or share your favorite lyrics" %>
Pretty ugly but least complicated
The newline character \n should be included between the double, however HTML does not allow for line feed, but Thomas Hunter suggested an hack which consists in using a bunch of white spaces, like so:
<%= f.text_area :comment, placeholder: "Comment on your track or share your favorite lyrics" %>
You can also opt to use the title attribute instead.
In Ruby, in general "\n" is the new line character.
e.g.:
puts "first line\nsecond line"
=>
first line
second line
However, in your case:
you seem to try to use the newline character in an .erb expression <%= ... %>
That does not work, because that will only format the newline in the raw HTML souce,
but in the formatted HTML you will not see the newline! :-)
To see the newline in the formatted HTML, you need to do something like this:
either put the two strings in separate DIVs or SPANs
or put a <br /> in the string instead of the "\n" -- <br \> is the HTML newline symbol
You're creating HTML code. HTML does not care about whitespace in the actual code. What you need is a break in the HTML itself. However, it seems per this other question Can you have multiline HTML5 placeholder text in a <textarea>? that HTML does not allow line breaks in the placeholder field.

simple_format and 2+ newline(\n)

I'm using rails and need to show text, having 3 or more newline characters in a row.
I found simple_format method, but it works with 2,3,4,... symbols identically
Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in < p > tags.
For example, my text is
1.9.3p0 :015 > Article.last.text
=> "1\n\n2\n\n\n\n33"
when i do <%= simple_format Article.last.text.html_safe %> it generates me this view:
<p>1</p>
<p>2</p>
# but i need <br/> or smth else there
<p>3</p>
Other solutions are welcome, thanks.
I might still be missing something, but why not just use string.gsub(a, b):
"1\n\n2\n\n\n\n33".gsub("\n", "<br />").html_safe # => "1<br/><br/>2<br/><br/><br/><br/>33"
Surely you can also pass the previous line to simple_format to have the line wrapped into a <p> tag.

How to allow newlines in ERB output

I'm trying to show the contents of a field from the database in a <p> element. In the html.erb template the code looks like:
<p><%= front.gsub(/(\r)?\n/, "<br>") %></p> ...
The issue I'm having is that to escape the breaks, I have to apply the .html_safe method at the end of the above gsub, but doing so opens the whole application to XSS attacks. How can I only allow the breaks to be escaped?
You can use the simple_formatmethod.
<%= simple_format(front) %>
More here => http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format
This is based on the simple_format helper. We can use sanitize to remove bad tags that allow XSS attacks.
<%= sanitize(front).gsub(/(\r)?\n/, "<br/>").html_safe %>
You can also use strip_tags if you want to remove all HTML tags before replacing new lines with <br>.
<%= strip_tags(front).gsub(/(\r)?\n/, "<br/>").html_safe %>
Have you considered wrapping the text into <pre>-tags instead? That will keep the basic formatting (newlines, spaces, ...).

Show <br/> in <p>

i replaced line feed characters with HTML line break as
#post.description.gsub(/\n/, "<br/>")
I am trying to show the output within tags. But I see as follows:
Used, like new book<br/>New book costs $150<br/>Awesome book!
Need help. Thanks.
Use simple_format for this as it will automatically convert new lines into breaks.
<%= simple_format(#post.description) %>

What does it mean to add a hyphen to the closing tag of a ruby loop <% -%>

I'm a noob at this and can't figure out why the hyphen gets added to something like this:
Not even sure if my jargon in the title of this question is accurate.
adding the "-" will remove the line break for that line
It means simply:
Place any text (HTML) that follows <% -%> on the next line in the rendered template.
The hyphen is not necessary in the above code. Simply adding <% end %> is sufficient to execute the embedded ruby.
The use of hyphen is fully explained here and essentially effects rendered html. In your case, what the hyphen does is this:
1 Hyphen at the end of the tag, just the two spaces
2 before the tag on the line below will be left
3 <% -%>
4 Last line
The code will output with two spaces before " Last Line" just below your <% -%> tag
It means that it will add the \n (or maybe \r\n, I forget which) to the line.
It just effects way the HTML is formatted.
So if did:
>> helper.image_tag "image.jpg"
=> "<img alt=\"Image\" src=\"/images/image.jpg\" />"
it would output something like:
"<img alt=\"Image\" src=\"/images/image.jpg\" />\r\n"
Meaning that your html page would look like:
<image tag>
<whatever other tag>
instead of having them both on the same line.

Resources