[ACCEPTED]-How to Show Error Messages Next to Field-haml

Accepted answer
Score: 40

You can use this

- if @resource.errors[:field_name]

Also useful link:



Score: 7

Just create a file in your initializers 2 folder.


Place this code in it:

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

PD: Sorry for 1 my english.

Score: 5

How about this

if you want to put the error 7 message just beneath the text field, you 6 can do like this

    = f.label :first_name, "*Your First Name:"
    = f.text_field :first_name, :required => true, class: "form-control"
    = f.error_message_for(:first_name)

What is error_message_for?
--> Well, this is a 5 beautiful hack to do some cool stuff

# 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(', ')}"\
      "<!-- Later JavaScript to add class to the parent element -->"\
          "document.onreadystatechange = function(){"\

Result enter image description here

Markup 4 Generated after error

<div id="error_user_first_name" for="user_first_name" class="signup-error alert alert-danger">This field is required.</div>
<script>document.onreadystatechange = function(){$('#error_user_first_name').parent().addClass('has-signup-error');}</script>

Corresponding SCSS

      background: transparent;
      color: $brand-danger;
      border: none;

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

          background-color: $bg-danger;
          border-color: $brand-danger;

Note: Bootstrap 3 variables used here and, do not forget to 2 Restart the server now and after any modification 1 to the file in config dir.

Score: 0

You can use error_message_on http://apidock.com/rails/ActionView/Helpers/ActiveRecordHelper/error_message_on


form.error_messages 2 was removed from Rails and is now available 1 as a plugin. Please install it with rails plugin install git://github.com/rails/dynamic_form.git.

Score: 0

If anyone is looking for a way how to display 2 error messages for particular field in Rails 1 6:

a = Post.new
a.save # => false
["Title can't be blank"]

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

More Related questions