simple_format and 2+ newline(\n) - ruby-on-rails

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.

Related

Can't render line breaks in Rails with simple_format

I'm aware that in order to render \r\n I need to use simple_format, however it doesn't work on my posts that I migrated from WordPress. I tried many solutions including regex to replace \r\n with break tags, but it didn't help either. I still see on the screen all the line breaks printed out as text and not rendered.
Here is what I tried:
<%= simple_format(#post.body) %>
<%= simple_format(#post.body.gsub(/(?:\n\r?|\r\n?)/, '<br>')) %>
If I just do something like below it will work.
<%= simple_format "<h1>Briefed while smartwatch firm Pebble lays off 25% of its staff</h1> -\r\n\r\n \r\n <p>hello</p>" %>
I have no idea what am I doing wrong.
Try following, it should works for you, I have tested, its working
> "\n\r".gsub(/[\r\n]+/, '<br>') => "<br>"
> "\r\n".gsub(/[\r\n]+/, '<br>') => "<br>"
In your case
(#post.body.gsub(/[\r\n]+/, '<br>')
I finally solved it using an SQL query:
UPDATE posts SET body = REPLACE(body, '\r\n', '<br>');
Don't know why Rails gsub didn't work.
Edit:
Looks like my regex was wrong. This solves it too:
<%= simple_format(#post.body.gsub(/\\r\\n/, "\n")) %>

How to prevent Rails from showing an empty line in the source code if condition is false?

In my Rails 4 app I have a TagHelper with this function:
def robots_tag
if important_page?
tag(:meta, :name => "robots", :content => "dofollow")
end
end
In my main layout I am using it like this:
<head>
<%= robots_tag %>
</head>
How can I prevent Rails from showing an empty line in the source code if important_page? is false?
Thanks for any help.
Try adding a minus sign at the end of the erb tag:
<%= robots_tag -%>
Just out of interest, why do you want to get rid of the newline? Even if it was in the body it wouldn't affect the result (visible to the user at least); in the head it seems even less important.
There's no way within the <%= %>-Statement to 'remove' the empty line, because there's a newline character just before (in the HTML, after the <head>-tag). If you really want to avoid an empty line, you need to remove this character by putting the ruby-tag/s right after the <head>-tag (the following code assumes the test for important_page? is removed from the method):
<head><% if important_page? %>
<%= robots_tag %><% end %>
</head>
(Side-note regarding the first answer: while the minus sign does remove the newline character, it doesn't remove the leading spacing, which results in just a different kind of ugly source code)

Spaces inserted in browser in <pre> with Rails

A simple test case:
<% content = "<pre>a\nb</pre>" %>
<%= raw content %>
Browser Screenshot, with inserted spaces on second line:
Here's where it gets interesting. In rails:
raw(content).size # => 14 (correct)
But, in JS:
$("pre").html().length // => 13 (should be 3)
Any chance you are using HAML for your layout? If so, that would explain it as HAML will indent things for you. You can use HAML's ~ to get around this.
See: http://haml.info/docs/yardoc/file.REFERENCE.html#tilde

Including contents of a variable inside a Haml filter

How to make article.content filtered by :textile ?
- #articles.each do |article|
%article.post
%header=article.name
%p
:textile
=article.content
%footer
the output
<article class="post">
<header>game</header>
<p>
</p><p>=article.content</p>
<p></p>
<footer></footer>
</article>
Inside a Haml filter you use String interpolation to include Ruby code in your markup. For example:
require 'haml'
#x = 42
Haml::Engine.new("%p= #x").render(self) #=> "<p>42</p>\n"
Haml::Engine.new(":textile\n\t= #x").render(self) #=> "<p>= #x</p>\n"
Haml::Engine.new(":textile\n\t\#{#x}").render(self) #=> "<p>42</p>\n"
#content = "alpha\n\n#hi **mom**"
Haml::Engine.new(":textile\n\t\#{#content}").render(self)
#=> "<p>alpha</p>\n<p>#hi <b>mom</b></p>\n"
Edit: My previous answer was misleading with respect to newlines in the content, due to my flawed testing. As seen above, newlines in included content are handled fine directly.
As such, your Haml template should simply look like this:
- #articles.each do |article|
%article.post
%header=article.name
:textile
#{article.content}
%footer
Note that I've removed your %p tag surrounding your markup, as Textile introduces its own paragraph wrappers (even for single-line content).
Your syntax seems fine, if everything else is getting rendered properly maybe it's a gem issue. Do you have RedCloth installed?
Edit: On second thought, what does the second line do? It might be the cause of your problem since I don't think %article.post is proper HAML syntax and the :textile filter is inside it

How to show string in xml format in erb file

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.

Resources