17

I have the following two _forms:

user form

<%= simple_form_for(@user, :url => @target) do |f| %>
  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>
      <ul>
      <% @user.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :email, :label => "User Email" %>
  <%= f.input :password, :label => "User Password" %>
  <%= f.input :first_name %>
  <%= f.input :last_name %>
  <%= f.button :submit %>
<% end %>

tenant form

<%= simple_form_for(@tenant, :url => @target) do |f| %>
  <% if @tenant.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@tenant.errors.count, "error") %> prohibited this tenant from being saved:</h2>

      <ul>
      <% @tenant.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <%= f.input :name, :label => 'Name', :required => true %>
  <%= f.input :billing_email, :label => 'Email', :required => true %>
  <%= f.input :country, :label => 'Country', :required => true %>
  <%= f.button :submit %>
<% end %>

I have come across the following post from stackoverflow f.error_messages in Rails 3.0

Here there is method so that error messages can be returned from the simple form by using f.error_messages but I have been unable to get this working as I am unsure whereabout this method should be saved. Anyone got any hints? The method is as follows:

class StandardBuilder < ActionView::Helpers::FormBuilder
  def error_messages
    return unless object.respond_to?(:errors) && object.errors.any?

    errors_list = ""
    errors_list << @template.content_tag(:span, "There are errors!", :class => "title-error")
    errors_list << object.errors.full_messages.map { |message| @template.content_tag(:li, message) }.join("\n")

    @template.content_tag(:ul, errors_list.html_safe, :class => "error-recap round-border")
  end
end
Community
  • 1
  • 1
Jay
  • 3,012
  • 14
  • 48
  • 99

1 Answers1

25

Just add error: false to your inputs this will not clear the css but will clear the inline errors

f.input error: false

Edit:

From http://ruby.railstutorial.org/book/ruby-on-rails-tutorial

/app/views/shared/_error_messages.html.erb

<% if object.errors.any? %>
    <div id="error_explanation">
        <div class="alert alert-error">
            The form contains <%= pluralize(object.errors.count, "error") %>.
        </div>
        <ul>
            <% object.errors.full_messages.each do |msg| %>
              <li>* <%= msg %></li>
            <% end %>
        </ul>
    </div>
<% end %>

in VIEW

<%= form_for(@user) do |f| %>
  <%= render 'shared/error_messages', object: f.object %>

  <%= f.label :name %>
  <%= f.text_field :name %>

  <%= f.label :email %>
  <%= f.text_field :email %>

  <%= f.label :password %>
  <%= f.password_field :password %>

  <%= f.label :password_confirmation, "Confirmation" %>
  <%= f.password_field :password_confirmation %>

  <%= f.submit "Create my account", class: "btn btn-large btn-primary" %>
<% end %>
engineersmnky
  • 25,495
  • 2
  • 36
  • 52
  • 2
    missing a <% end %> on that first block: ` <% if object.errors.any? %>
    The form contains <%= pluralize(object.errors.count, "error") %>.
      <% object.errors.full_messages.each do |msg| %>
    • * <%= msg %>
    • <% end %>
    <% end %>`
    – TomDunning May 15 '13 at 16:02
  • 2
    I like this implementation of using the partial for the form errors, cool! – JP Silvashy May 21 '13 at 08:47
  • Using `
  • * <%= msg %>
  • ` is a bit weird since the `
  • ` already produces a bullet. Might consider removing it, i.e. using `
  • <%= msg %>
  • ` instead. – adamlamar Dec 02 '14 at 23:07
  • @adamonduty depends on your css. I wanted `*` instead of bullets as I feel it lends more to the importance of an item. – engineersmnky Dec 03 '14 at 14:41
  • 1
    Tip: if you put the partial in `app/views/application` instead of `app/views/shared` then you can do `<%= render 'error_messages', object: f.object %>` instead of `<%= render 'shared/error_messages', object: f.object %>`. – Jason Swett Dec 06 '18 at 19:28