Ruby on Rails error messages

I made validates_presence_of fields for comments in the guide . How to display validation errors on a message page. I know about the render, but with the render I need to send all the variables that I use on the message page. I'm right?

+3
source share
4 answers

I do not believe that a plugin is needed. The scaffold generator in Rails 3.x does this as follows:

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

      <ul>
      <% @model.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<%# Rest of form... %>
+4
source

If you want to show messages in your view, for example:

Rails UI for error_messages

Is this what you want. More information http://www.monochrome.co.uk/blog/2010/04/14/rails-3-error_messages-and-error_messages_for

`rails plugin install git://github.com/rails/dynamic_form.git`

<%= form_for @supermoon do |f| %>
  <%= f.error_messages %>

A plug is not required in rails 2.3 (required in 3.x)

+2

( ) @variable.errors.full_messages, .

+1

validates_presence_of has an argument: message to generate an error message.

class Person < ActiveRecord::Base
    validates_presence_of :user_name, :message => 'My custom error message'
end
0
source

All Articles