Rails 3. simple_format do not wrap result in paragraph tags
Unfortunately -- you can't. If you check out the source at http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#method-i-simple_format you'll see that the p tags are wrapped around the content unconditionally.
You could create a helper that uses the simple_format code, but modify it to not include the p tags...
Rails: simple_format alternatives?
I do have one solution but I'm willing to bet you have already searched the page on the helpers here? From there you can scroll down to ":wrapper_tag" which gives you at least some form of customization (minimal at best). It let's you wrap a tag of your choice which is then wrapped by the paragraph tag. I found another answer here. The first response there says "Unfortunately -- you can't...p tags are wrapped around the content unconditionally".
But, scroll down on the same page and you'll see that someone created a helper method that might help you.
For the time being, I just stopped using simple format as I'm not making blogs or anything that would need that. Also another tip, maybe you could just use a sanitizer and customize like the response linked above.
Hope that helped! I know that it probably wasn't the answer you were looking for but I guess it's a start.
UPDATE 7/26 11PM
Looks like the person in the second article wasn't the first to create a separate helper. I found this and while it is from Rails 3, it should help you:
def simple_format_no_tags(text, html_options = {}, options = {})
text = ” if text.nil?
text = smart_truncate(text, options[:truncate]) if options[:truncate].present?
text = sanitize(text) unless options[:sanitize] == false
text = text.to_str
text.gsub!(/\r\n?/, "\n") # \r\n and \r -> \n
text.gsub!(/([^\n]\n)(?=[^\n])/, ‘\1<br />’) # 1 newline -> br
text.html_safe
end
How Can I Convert \r \n to p with simple format in Ruby on Rails?
You might be better off doing a string replace first:
@html.gsub!(/\r\n?/, "\n");
body = simple_format(@html)
This because simple_format only works on \n and \n\n, not \r\n:
From the API:
Returns text transformed into HTML using simple formatting rules. Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped in
tags. One newline (\n) is considered as a linebreak and a
tag is appended. This method does not remove the newlines from the text.
Rails: Keep simple_format from adding line breaks to lists?
As far as i'm aware, simple_format is designed to just work around line breaks '\n' (which are created when you press the enter key)
simple_format creates a <br />
for \n and <p>
for \n\n
simple_format is not really designed to work with existing html input but more for text_area fields from databases and text strings etc.
see rails docs for examples: ActionView - simple_format
A couple of suggestions could be just wrap your entire input using content_tag:
<%= content_tag :p, :class => "strong" do -%>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
<% end -%>
or maybe if possible format your html without linebreaks:
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
<ul> <li>Item 1 </li> <li>Item 2</li> <li>Item 3</li> </ul>
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
These are both pretty hackish, the best solution would be to have all the input stored as html before it gets called to be displayed. But not knowing how your obtaining the input, it is hard to suggest a way to do that.
Extra line breaks with simple_format
I’m going to assume, that because you are wrapping the body content in show.html.erb, that you don’t mind the data in your database but just want to clean it up to present it.
Have you tried:
<p><%= strip_tags @post.body %></p>
Rails API Reference
Rails: How to save formatted text in text_area?
You should use text editor for example ckeditor (to simplify web content creation), and in view try simpleformat or raw:
<%= simple_format("Here is some basic text...\n...with a line break.") %>
<%= raw("Here is some basic text...<br/>...with a line break.") %>
Preserve newline in text area with Ruby on Rails
Newlines are actually being preserved(as \r\n
), you just don't see them in your index/show views.
In these views, call simple_format
on your post.body
field to replace \n
s with <br>
s(HTML newlines):
simple_format(post.body)
From docs:
simple_format(text, html_options = {}, options = {}) public
Returns text transformed into HTML using simple formatting rules.
Two or more consecutive newlines(\n\n) are considered as a paragraph and wrapped
in <p> tags. One newline (\n) is considered as a linebreak and a <br /> tag is
appended. This method does not remove the newlines from the text.
Related Topics
Is the .Each Iterator in Ruby Guaranteed to Give the Same Order on the Same Elements Every Time
Session Not Destroyed When Closing Browser - Railstutorial.Org
How to Do Ruby Object Serialization Using JSON
Modifying the Default Hash Value
Install Nokogiri 1.6.1 Under Ruby 2.0.0P353 (Rvm Based Installation) Fails (Osx Mavericks)
Converting a Unique Seed String into a Random, Yet Deterministic, Float Value in Ruby
Rails Cannot Load Such File -- MySQL2/Mysql2 (Loaderror)
Rails Error Method_Missing': Undefined Method 'This' for Gem::Specification
Access Current Git Commit Number from Within Heroku App
Rails 3 Cli Executes Commands Really Slow
Optional Parens in Ruby for Method with Uppercase Start Letter
How to Use Same Browser Window for Automated Test Using Selenium-Webdriver (Ruby)
How to Rename or Move Rails's Readme_For_App
Rails 3, Displaying Jpg Images from the Local File System Above Rails_Root
How to Has_And_Belongs_To_Many Multiple Instances of the Same Model
Generate Nested Hashes from Strings and Deep Merging in Ruby