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")) %>
Related
Using Meta-Tags gem, in my blog post show page this piece of code prints the code itself instead of showing its value, which is the blog post title!
<% title '#{#blog_post.title}' %>
What's wrong with it? Seems like such easy problem but haven't come up with a solution yet!
Try this:
<% title "#{#blog_post.title}" %>
If you need to have ruby interpolation, then you need to use double quotes, not single quotes.
Try changing this:
<% title '#{#blog_post.title}' %>
to this:
<% title "#{#blog_post.title}" %>
Then it should work.
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
I actually solved this using regex but I want to know if there's an easier to get around it as I'm not confident in my regex if I covered all possible rules. My problem is that I want to put double quotes (") around a text and print it. The text has html tags when retrieved in the database.
let's say I'm working on this,
#text = `<p>my <strong>legs <em>wants</em> </strong>ex <strong>desire</strong> blood sleep</p>`
When I tried <%= raw "\"#{#text.html_safe}\"" %> in my view, I'm getting this as my output:
"
my legs wants ex desire blood sleep
"
When I did something like <%= "\"#{#text.html_safe}\"" %> the output is:
"<p>my <strong>legs <em>wants</em> </strong>ex <strong>desire</strong> blood sleep</p>"
Any ideas? All i wanted to print out was "my legs wants ex desire blood sleep" literally
UPDATE
using strip_tags was cool and I thought it got me nearer to the answer, but unfortunately strip_tags isn't working in my controller/regular ruby. I'm still researching.
require 'nokogiri'
doc = Nokogiri('<p>my <strong>legs <em>wants</em> </strong>ex <strong>desire</strong> blood sleep</p>'.gsub(' ', ' '))
# use Nokogiri to get plain text
# output it
= "\"#{doc.text.squeeze}\""
I think you want strip_tags from the docs
<%= "\"#{strip_tags(#text)}\"" %>
Edit:
Uglier, but gets rid of the  , too.
<%= "\"#{strip_tags(#text.gsub(" ", "")}\"" %>
Try .strip:
<%= "\"#{#text.html_safe.strip}\"" %>
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 <embed> video link like youtubes
It's OK to use <%= simple_format #post.content %> and <%= #post.content.html_safe %> separately
But when I use them together: <%= simple_format #post.content.html_safe %>, html_safe is not working, and hence the <embed> video is not displayed
Could you tell me how can I enable <embed>code and simple_format at the same time? or is there other solutions to display the #post.content? Thanks!!!
I'd tell simple_format not to sanitize my content:
simple_format(#post.content, {}, :sanitize => false)
I am working on a similar problem.
I am trying to post code snippets in my blog post. It works pretty well but anything inside a <> gets stripped out. Whether I show or something more complex anything inside the <> disappears. I have run the <%= simple_format(#article.content), {}, sanitize: false code and I came close to getting what I wanted.
The problem was the code inside my blocks actually altered my page layout. :).
I wound up going with Redcarpet as my answer.
It's pretty simple.
Add gem 'redcarpet' to your Gemfile and restart your Rails server.
In the application_helper.rb put the following code:
def markdown(content)
#markdown ||= Redcarpet::Markdown.new(Redcarpet::Render::HTML, autolink: true, space_after_headers: true, fenced_code_blocks: true)
#markdown.render(content)
end
The options here are described in the documentation. But, fenced_code_blocks: true is what allows you to put code in blocks as described.
That will output here whatever you type and it will work with your embed.
Then, to render it in your case just put:
markdown(#post.content).html_safe
Should be good to go. You also have the option to indent four spaces like here to insert code. It seems easier to do the fenced though.
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.