Why does the simple_format helper seem to ignore double new lines in ruby on rails? - ruby-on-rails

I have a micropost feature and was testing the way it formats text that has been posted when displaying back to the user.
I pasted the following text like this:
and this was displayed back to me:
I'm using "simple_format h(content)". When I remove the helper the text is displayed with out a new line from the word "In". It displays as one big paragraph so I assume the helper is working but for some reason my double new lines are being ignored.
Any idea what is going on? Am I missing something?

By seeing it back, do you mean inside a textarea, or on the page? If it's on the page, all whitespace is compressed to one space each. If it's the latter, simply use the css rule:
white-space:pre;
On the proper selector.
However, if it is in a textarea (which preserves whitespace by default), there must be something stripping the extra space when you save it into the database. You might want to debug down your stack in the model & controller, to see where this might be happening. I have to admit i haven't used the the simple_format method.

Thanks to chrome developer tools as per usual. I realised that each text separated by 2 new lines were wrapped with p tags so I just added a bottom margin of 5px using css to p. Works perfectly.

Related

ruby on rails: text filed formatting

I tried to copy and past from Word document to text field using Ruby on Rails.
But all formatting( spaces, bold and other) are disappeared on text filed.
I've just got the simple lines of text without any formatting.
I've read that need to use Simple format tool... but I want that a user be able just to copy and past a text to text field without doing any adjustments.
I mean, I want make all adjustment in advance and the user could just copy and past the text and got all formatting, the same as in Word doc.
The link to file with text field as below.
https://gist.github.com/tatyana12/2f9d39c2f6e4f8fabea5e70e11eaf310
Also I have Application.html.erb file:
https://gist.github.com/tatyana12/15c27d542091b04f3c3adfdfd252b7f4
How to initialize editor if I don't have id = "edit" right now?
How to put some code extra style to this file?
Have a go at wrapping your field in your show.html.erb or wherever you want to display it with simple_format, for example:
<%= simple_format(#object.description) %>
See http://apidock.com/rails/ActionView/Helpers/TextHelper/simple_format for more info.
I've solved this problem by implementing SKEditor.
There is a lot of tutorial how to implement this editor.
I (as a lot of other users) have problem that my text wasn't formatted because this editor is not compatible with turbo links.
So, I disable turbo links in some files, and as result I have the text formatted.

How to supply hardcoded multiline content in h:inputTextarea

In HTML I can do:
<textarea>
line 1
line 2
line 3
</textarea>
However, the same thing doesn't work with the JSF <h:inputTextarea>, as I have to specify the value attribute instead of the body. Is there a way that I can specify a multiline value? Ideally I would like to be able to do it as seamlessly as in HTML, ie with no \n and actually putting it onto multiple lines, for easy readability.
You could try this :
<h:outputFormat escape="false" value="Line 1<br/>Line 2" />
Which would render:
Line 1
Line 2
Note:The escape="false" means you can embed html, like lists etc...
or else we can even try like this:
Alternatively you could set readonly to true on the inputTextArea and set the rows attribute.
I Hope this will help you
Well, I couldn't find a way to include the textarea content directly into the .xhtml, while keeping the content on several lines. There's probably a way with some javascript by using a different tag and then copying it in or something, but I wanted to keep it fairly clean.
What I ended up doing was using the value attribute of h:inputTextarea and I populated it from my managedBean. I created text files of the content I wanted (they were quite big files) and loaded them into the managedBean property using a FileReader. When you assign them to the value of the h:inputTextArea, the \n etc are respected, so that it gives you the identical effect to if you had hardcoded the text into the .xhtml between textarea tags.

Input from textarea has weird spaces prepended to each line in simple form and haml

I am building a ruby on rails application using simple form and haml for the views. However I've noticed an issue with regards to forms that have textareas. I didn't notice it at first however when I save some text in a textarea and dump it in a pre tag I've noticed that every line break is followed by a number of ugly white spaces. I'm not sure whats going on here. So if I enter something like the following in a textarea:
THIS IS ONE LINE
THIS IS ANOTHER LINE
THIS SHOULD NOT BE INDENTED
THIS SHOULD BE INDENTED BY TWO SPACES
Outputting it into a pre tag gives:
THIS IS ONE LINE
THIS IS ANOTHER LINE
THIS SHOULD NOT BE INDENTED
THIS SHOULD BE INDENTED BY TWO SPACES
I'm pretty lost here - this is my basic textarea code - nothing much(added the tilde after hearing that it would help in removing unwanted spaces - didn't work at all):
~f.input :details, :label=>false, :input_html=>{:class=>'span12 input-code', :rows=>40}
Another observation - the spaces do not show up in the textarea if I render the text area using simple form - however if I manually code in a textarea to output the contents it shows the spaces. Could this have anything to do with the simpleForm gem?
I know this is a very old post but I came here looking for a solution to the exact same issue so I thought on sharing the solution I found on another link in case someone comes here too...
For me this happened on my Rails 5 app, that uses simple_form and HAML.
The reason of the additional spaces is HAML, and in order to avoid them, you need to add to the config/initializers/haml.rb the following line:
Haml::Template.options[:remove_whitespace] = true
Here's the source of my answer (check the very last comment by adavia): https://github.com/haml/haml/issues/643
And just in case, here's the list of options HAML allows: http://haml.info/docs/yardoc/Haml/Options.html
Edit
This option also works:
Haml::Template.options[:ugly] = true

Rails]Using Markdown Markup Language

In my rails application, people are supposed to submit "posts." However, in the default scaffolding, there are some problems in the text input: not allowed HTML code, changing the line doesn't work, etc. From what I've learned, I need to use a markdown-markup language to solve this issue. Is there a guide for me to follow to apply such language to solve my problem?
UPDATE: Here are my problems.
1) Every sentence is combined into one line even if I put a line space.
first line
second line
becomes
first line second line
2) I can't make text bold, italicized, or hyperlink. Like in stackoverflow, user should easily put <b> and make bold text, ** to make italicized, etc. And URL address should automatically be translated to href link.
To do these, I thought I had to use markdown library. I could be mistaken, so I needed someone to guide me through. Railscasts on Markdown
Well, yes, new lines in HTML have no meaning. You need to replace line breaks with <br> to preserve them in HTML. To automatically highlight links, you need to look for links in the text and wrap them in appropriate <a> tags. Finally, if you're not filtering HTML tags, they should still be in there. It all depends on what you're doing. Markdown is something entirely different, a special markup language that enables you to do the above while being easier to write than HTML. It depends on what you want to use.

trouble using tinymce using ruby on rails

I am having trouble in using tinymce editor with rails 3. I want to show text in bold letters and having trouble using tags like when I write something in p tags It should go to next paragraphs. in my case this tags is not working. It remains on same lines and display p tags on site page.
The usual suspect when it comes to rails 3 printing raw html output to the site, is that someone forgot to call html_safe on whatever text should be printed.
So if you have a #my_model_instance.description that you edit with tinymce, you might want to make the view look like #my_model_instance.description.html_safe, or as they suggest in the comment on the documentation, raw(#my_model_instance.description).
If the text is coming from user input, however, you might want to be a bit cautious, since it might be possible for users to input all sorts of nasty injection hacks this way.

Resources