Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.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 RubyonRails在特定字段中存在错误_Ruby On Rails_Ruby_Twitter Bootstrap - Fatal编程技术网

Ruby on rails RubyonRails在特定字段中存在错误

Ruby on rails RubyonRails在特定字段中存在错误,ruby-on-rails,ruby,twitter-bootstrap,Ruby On Rails,Ruby,Twitter Bootstrap,我正在定义“Contacts”表中某些字段的验证: class联系人 “表单控件”%> 但当我登录到此页面时,该字段已为红色。是否有一种方法可以查看在一条记录的验证过程中,某个特定字段是否出现错误 与@contact.errors[:name]?“has error”:“,如果@contact,您正在打印has error。errors[:name]不是零。我想它只是空的 尝试执行@contact.errors[:name]。空白?“:”有错误“,这意味着如果它为空(相当于“为零或为空”),则

我正在定义“Contacts”表中某些字段的验证:

class联系人
我想利用引导的类属性“has error”在出现错误时以红色突出显示特定字段;所以我试着这样做:

  <div class="form-group <%= @contact.errors[:name] ? "has-error" : "" %>" >
    <%= f.label :name , :class => "control-label" %><br>
    <%= f.text_field :name, :class => "form-control" %>
  </div>

“控制标签”%>
“表单控件”%>

但当我登录到此页面时,该字段已为红色。是否有一种方法可以查看在一条记录的验证过程中,某个特定字段是否出现错误

@contact.errors[:name]?“has error”:“
,如果
@contact,您正在打印
has error
。errors[:name]
不是零。我想它只是空的


尝试执行
@contact.errors[:name]。空白?“:”有错误“
,这意味着如果它为空(相当于“为零或为空”),则不要添加该类,否则请添加所需的类。

@contact.errors[:name]
返回消息数组,如果其中没有消息,则
@contact.errors[:name]
将只返回空数组。数组是一个对象,因此您的语句将始终产生
“has error”

这将有助于:

<%= @contact.errors[:name].present? ? "has-error" : "" %>


试试这是结果点击此方法加载页面时,类直接分配给字段这接近解决方案,但仍然不起作用。它总是将“”属性分配给类
<%= @contact.errors[:name].present? ? "has-error" : "" %>