Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 如何在字段旁边显示错误消息_Ruby On Rails_Devise_Haml - Fatal编程技术网

Ruby on rails 如何在字段旁边显示错误消息

Ruby on rails 如何在字段旁边显示错误消息,ruby-on-rails,devise,haml,Ruby On Rails,Devise,Haml,我有一个带有输入字段/标签等的表单。如何让错误消息显示在字段旁边?而不是在顶部聚集在一起 我正在使用Desive,rails 3 我的表格顶部有: = form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| - if resource.errors.any? #errorExplanation %h2 = pluralize(resou

我有一个带有输入字段/标签等的表单。如何让错误消息显示在字段旁边?而不是在顶部聚集在一起

我正在使用Desive,rails 3

我的表格顶部有:

 = form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f|
- if resource.errors.any?
  #errorExplanation
    %h2
      = pluralize(resource.errors.count, "error")
      prevented this user from being saved:
    %ul
      - resource.errors.full_messages.each do |msg|
        %li
          = msg

您可以在上使用错误消息

更新: form.error_消息已从Rails中删除,现在可以作为插件使用。请使用
rails插件安装来安装它git://github.com/rails/dynamic_form.git

您可以使用此

- if @resource.errors[:field_name]
  ...
另一个有用的链接:


只需在initializers文件夹中创建一个文件

config/initializers/inline_errors.rb

将此代码放入其中:

ActionView::Base.field_error_proc = Proc.new do |html_tag, instance|
  unless html_tag =~ /^<label/
    %{<div class="has-error">#{html_tag}<span class="help-block">#{instance.error_message.first}</span></div>}.html_safe
  else
    %{#{html_tag}}.html_safe
  end
end
ActionView::Base.field_error_proc=proc.new do | html_标记,实例|
除非html_tag=~/^这个怎么样

如果要将错误消息放在文本字段下方,可以这样做

.row.spacer20top
  .col-sm-6.form-group
    = f.label :first_name, "*Your First Name:"
    = f.text_field :first_name, :required => true, class: "form-control"
    = f.error_message_for(:first_name)

什么是
错误消息?
-->嗯,这是一个很好的黑客做一些很酷的东西

# Author Shiva Bhusal
# Aug 2016
# in config/initializers/modify_rails_form_builder.rb
# This will add a new method in the `f` object available in Rails forms
class ActionView::Helpers::FormBuilder
  def error_message_for(field_name)
    if self.object.errors[field_name].present?
      model_name              = self.object.class.name.downcase
      id_of_element           = "error_#{model_name}_#{field_name}"
      target_elem_id          = "#{model_name}_#{field_name}"
      class_name              = 'signup-error alert alert-danger'
      error_declaration_class = 'has-signup-error'

      "<div id=\"#{id_of_element}\" for=\"#{target_elem_id}\" class=\"#{class_name}\">"\
      "#{self.object.errors[field_name].join(', ')}"\
      "</div>"\
      "<!-- Later JavaScript to add class to the parent element -->"\
      "<script>"\
          "document.onreadystatechange = function(){"\
            "$('##{id_of_element}').parent()"\
            ".addClass('#{error_declaration_class}');"\
          "}"\
      "</script>".html_safe
    end
  rescue
    nil
  end
end
注意:这里使用的引导变量 而且,不要忘了在config dir中对文件进行任何修改后,立即重新启动服务器


如果有人想知道如何显示Rails 6中特定字段的错误消息:

a = Post.new
a.save # => false
a.errors.full_messages_for(:title)
["Title can't be blank"]

a.errors.full_messages_for(:title).join(', ')
"Title can't be blank, Title too short"

美好的看起来很棒!要获取消息,我会说@resource.errors[:field\u name].ful\u消息吗?谢谢,这会在一行上给我一个错误列表吗?我正在尝试做ffg:对于每个字段,如果该字段上有错误,为每个错误创建一个div并将其输出到该字段下,或者创建一个包含错误列表的div,在另一行下1必须有属性不能为空行2必须有属性不能短于5个字符等等,它将返回一行错误。您可以通过询问
@resource.errors[:field_name].来检查是否存在错误。是否存在?
不是我真正需要的,我需要多行-我有这个-if-resource.errors[:firstname]。有吗?-resource.errors[:firstname].each do | msg |%div.error First name=msg…这很好,但我想将其放入帮助程序中,这样我就不会在每个字段重复它了…没有提供真实的描述来引导读者找到答案。这看起来非常有用。我放了一些文字来提供一些信息,我的英语很差,因此我以前没有写过一些文字:/Thank@el_quick。你是否有一个例子说明你是如何在表格中使用这个的?我正在努力弄清楚你到底是如何实现这一点的。顺便说一句,我改变了评级:-)我认为
field\u error\u proc
不是rails公共API的一部分,因为它没有文档记录。如果是私有的,这是一个危险的解决方案。这个问题问得好,但我认为对于UJS表单来说,答案并不优雅
  .has-signup-error{
    .signup-error{
      background: transparent;
      color: $brand-danger;
      border: none;
    }

    input, select{
      background-color: $bg-danger;
      border-color: $brand-danger;
      color: $gray-base;
      font-weight: 500;
    }

    &.checkbox{
      label{
        &:before{
          background-color: $bg-danger;
          border-color: $brand-danger;
        }
      }
    }
a = Post.new
a.save # => false
a.errors.full_messages_for(:title)
["Title can't be blank"]

a.errors.full_messages_for(:title).join(', ')
"Title can't be blank, Title too short"