Rails - line breaks occurring between fields of a form with errors

I'm having trouble trying to get rid of extra line breaks. Rails seems to insert between fields with errors.

I created a new rails application, created a stage called "users" with a name and age, and then said validates :name, :presence => true and validates :age, :presence => true . Then I loaded the users/new page and simply clicked “submit” without entering anything into the fields to create the error page. It so happened that between the label "name" and the field for entering the name was added an additional line break. Same thing with the label "age" and its field. How to stop this extra line break?

+4
source share
2 answers

Ach, just bit him.

When you have form fields with errors, the rails change the output of auxiliary form methods such as #label and #text_field.

As a result, your pretty little “label” and “enter” labels are still emitting - just “stealth” wrapped around the surrounding div. For instance:

 f.label :name 

:

 <label for="name">Name</label> 

in

 <div class="field_with_errors"><label for="name">Name</label></div> 

The default behavior for a div is a “block” that causes line breaks.

You can fix this by modifying css. As an example:

 div.field_with_errors { display: inline; } 
+10
source
 .field_with_errors:nth-child(odd) { padding: 2px; display: table; } .field_with_errors:nth-child(even) { padding: 2px; display: inline; } 

This is what I end up changing because extra line breaks were good on shortcuts, but I didn't want lines to break in real form fields.

0
source

Source: https://habr.com/ru/post/1385216/


All Articles