Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails -- line breaks appearing between form fields with errors

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

I created a new rails app, created a scaffolding called "users" with a name and an age, and then said validates :name, :presence => true and validates :age, :presence => true. Then I booted up the users/new page and just clicked "submit" without entering anything in the fields to generate the error page. What happened was that between the "name" label and the field for entering the name, an extra line break was inserted. Same with the "age" label and its field. How do I stop this extra line break from happening?

like image 256
Kvass Avatar asked Dec 27 '22 09:12

Kvass


1 Answers

Ach, just got bitten by this one too.

When you have form fields with errors rails changes the output of form helper methods like #label and #text_field.

The result is your nice little "label" and "input" tags are still being emitted - just "stealth" wrapped with a surrounding div. For example:

f.label :name

goes from:

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

to:

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

The default behavior of a div is "block" - which is causing your line breaks.

You can fix this by changing the css. As an example:

div.field_with_errors {
  display: inline;
}
like image 111
TreyE Avatar answered Mar 08 '23 06:03

TreyE