Rails -- 出现错误的表单字段之间的换行符 [英] Rails -- line breaks appearing between form fields with errors

查看:41
本文介绍了Rails -- 出现错误的表单字段之间的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在试图摆脱 Rails 似乎在有错误的字段之间插入的额外换行符时遇到了麻烦.

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

我创建了一个新的 rails 应用程序,创建了一个名为users"的脚手架,带有名称和年龄,然后说 validates :name, :presence =>true 验证 :age, :presence =>真的.然后我启动了 users/new 页面,然后点击提交"而不在字段中输入任何内容来生成错误页面.发生的事情是在名称"标签和用于输入名称的字段之间插入了一个额外的换行符.与年龄"标签及其字段相同.我如何阻止这个额外的换行符发生?

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?

推荐答案

哎呀,也被这个咬了.

当表单域有错误时,rails 会改变表单辅助方法的输出,比如 #label 和 #text_field.

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

结果是你漂亮的小标签"和输入"标签仍在发出 - 只是隐身"用周围的 div 包裹.例如:

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

来自:

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

到:

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

div 的默认行为是block"——这会导致换行.

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

您可以通过更改 css 来解决此问题.举个例子:

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

div.field_with_errors {
  display: inline;
}

这篇关于Rails -- 出现错误的表单字段之间的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆