Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 以这种方式设置错误消息html视图?_Ruby On Rails_Ruby On Rails 3_Ruby On Rails 3.1 - Fatal编程技术网

Ruby on rails 以这种方式设置错误消息html视图?

Ruby on rails 以这种方式设置错误消息html视图?,ruby-on-rails,ruby-on-rails-3,ruby-on-rails-3.1,Ruby On Rails,Ruby On Rails 3,Ruby On Rails 3.1,我正在使用并希望生成正确的html,以显示错误视图在主站点上的显示方式,即: 上述字段的html为: <div class="control-group error"> <label for="inputError" class="control-label">Input with error</label> <div class="controls"> <input type="text" id="inputError">

我正在使用并希望生成正确的html,以显示错误视图在主站点上的显示方式,即:

上述字段的html为:

<div class="control-group error">
 <label for="inputError" class="control-label">Input with error</label>
  <div class="controls">
   <input type="text" id="inputError">
  </div>
</div>

我该怎么做才能让它像前者一样工作

不要再发明轮子了。使用。gem的当前版本允许您执行以下操作:

rails generate simple_form:install --bootstrap

这样,您就可以使用简单的表单帮助程序。它们将为您生成正确的内容。

Rails会在出现错误消息时自动生成一个带有类
字段\u with \u errors
的div。该div错误地包装了元素。要自定义它,您可以将此行添加到
application.rb

config.action_view.field_error_proc = Proc.new { |html_tag, instance| %Q(<div class="field_with_errors">#{html_tag}</div>).html_safe }
config.action_view.field_error_proc=proc.new{html_标记,实例}%Q({html_标记}).html_safe}
这是默认设置,因此为了获得与Twitter引导相同的结构,您可以使用它

html\u标记
是带有错误的字段的占位符,例如

您可以将其包装在另一个div中,还可以添加一个span,表示“请更正错误”


更多信息:-item 3.9

我刚刚遇到了这个问题,并通过对引导CSS的简单修改修复了它

我通常的字段代码是:

<div class="control-group">
    <%= f.label :fieldname, t('models.model.fieldname'), :class => "control-label" %>
    <div class="controls">
        <%= f.text_field :fieldname, :class => 'input-large' %>
    </div>
</div>
为了使它们具有与Bootstrap的
样式相同的外观,我在
Bootstrap.css
中添加了一些额外的选择器。我找到了对
.control-group.error…
的所有引用,并添加了带有
.control-group.field的重复行,带有错误…
。所以我就这样结束了:

.control-group.error > label,
.control-group.error .help-block,
.control-group.error .help-inline,
.control-group .field_with_errors > label,
.control-group .field_with_errors .help-block,
.control-group .field_with_errors .help-inline {
  color: #b94a48;
}
对于Rails来说,这可能不是最优雅的方法,但对我来说,这似乎比依赖更多的gems或覆盖错误处理要容易得多。是的,您必须在每次更新引导时进行相同的更改,但这些更改相当简单,您可能需要制作一个补丁文件来自动完成

<div class="control-group">
    <div class="field_with_errors"><label class="control-label" for="model_fieldname">Field name</label></div>
    <div class="controls">
        <div class="field_with_errors"><input class="input-large" id="model_fieldname" name="model[fieldname]" size="30" type="text" value=""></div>
    </div>
</div>
.control-group.error > label,
.control-group.error .help-block,
.control-group.error .help-inline,
.control-group .field_with_errors > label,
.control-group .field_with_errors .help-block,
.control-group .field_with_errors .help-inline {
  color: #b94a48;
}