I created an app with a posting function in Ruby on Rails. Implement the form on the post screen using the Rails helper method form_with method. At that time, I created an input field in text_area that allows input of multiple lines, so when I entered a sentence including line breaks and tried to display it, the sentence without line breaks was displayed. It was. This time I investigated how to solve the problem, so I will leave it in the article.
macOS Catalina version 10.15.7 Ruby 2.6.5 Ruby on Rails 6.0.3.4
The code of the form is as follows.
Ruby:app/views/shared/_form.html.erb
<%= form_with(model: word, local: true) do |f| %>
<%= f.text_area :remarks, placeholder: "Remarks", rows: "7" %>
<%= f.submit "Register" %>
<% end %>
Just in case, I used Rails' debugging tool pry-rails to check if the data sent by this form contains line breaks. First, I submitted the text including the following line breaks in the form.
Good morning
Hello
Here, the processing stops once at the binding.pry described in the controller, so when I executed params on the console and checked the parameters sent, As you can see, the line feed code "\ r \ n" is included in the line feed part, and it is properly sent as data.
And the code to display the sent data is below,
Ruby:app/views/show.html.erb
<%= @word.remarks %>
If this is left as it is, line breaks will not be included and the display will look like the one below.
Good morning Hello
Here are two ways to solve this problem!
I referred to the following site.
** simple_format is a Rails helper method ** and has the following functions.
**
**
Now, add simple_format to show.html.erb.
Ruby:app/views/show.html.erb
<%= simple_format(@word.remarks) %>
And when I update the browser and check it again,
Good morning
Hello
It was displayed with line breaks like this!
If you look at the Elements panel in Chrome's developer tools, Certainly, the string is enclosed in p tags, and line breaks are tagged with br.
Therefore, I tried to investigate the case of several consecutive line breaks as shown below.
Good morning
Hello
However, when I check the display,
Good morning
Hello
As you can see, consecutive line breaks didn't look exactly as you typed them. By the way, when I checked the Elements panel, the p tag was added as shown below. So, instead of this case is a new line, it will each "good morning" and "Hello" is ** display as ** one paragraph.
I referred to the following site.
The safe_join method is also one of Rails' helper methods. Unlike simple_format, you can display continuous line breaks without enclosing them in p tags.
First, rewrite the previous description as follows.
Ruby:app/views/show.html.erb
<%= safe_join(@word.remarks.split("\n"),tag(:br)) %>
Good morning
Hello
The display is exactly what you typed! !! If you check the Elements panel, You can see that the line breaks with the br tag.
While researching various things, I saw articles such as HTML, tags, and escaping. At the same time as I deepened my understanding of these things, I had many opportunities to come into contact with unknown knowledge, so I would like to further derive and deepen my knowledge.
I would appreciate it if you could point out any mistakes.
Recommended Posts