W
W
WEB_champion2014-11-03 23:38:11
Ruby on Rails
WEB_champion, 2014-11-03 23:38:11

How to display validation error message in Rails?

If validation is present in the User model, such as:
validates :name, :login, presence: true, then if this validation is not observed, errors should be displayed.
How can I display a list of errors in the view and where are these messages located in order to change them ???

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrew Emelianenko, 2014-11-04
@WEB_champion

Example of error output:

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

Read about changing error messages here: rusrails.ru/rails-internationalization-i18n-api

A
Alexander Wolf, 2014-11-03
@mannaro

rusrails.ru/active-record-validations

K
Kane, 2014-11-03
@Kane

@user.errors.full_messages.join(', ')

F
FanKiLL, 2014-11-04
@FanKiLL

Make yourself a partial, for example _error_messages.html.erb
, and then before each form, where errors will be displayed, render this partial.
for example you have a registration form

<%= form_for(@user, url: register_attempt_path, html: { class: 'form-wrapper form-medium' }) do |f| %>
    <%= render 'shared/error_messages', object: f.object %>
<%= f.text_field :username, class: 'form-control margin_form_control', placeholder: 'username' %>
<% end %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question