Error wit errrs

I have an app which I have upgraded to rails3. It is working fine but
for displaying error messages.

Take i.e. new.html.erb. In the model I have several validations that
work, they do not create a user if the entries are wrong. On submission
it dutifully returns to new.html.erb but WITHOUT the nice red error
messages.

I have been using

  <% 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 %>

but that doesn't help. I think the problem may be deeper. Do you know
what files are used when displaying the errors? Where does the error
variable come from?. I have scaffold.css

Could it be a js problem? I am using jquery so do not have prototype or
scriptaculous. Could that be causing the problem?

The way you want to display the errors looks cumbersome to me. But maybe
you have a good reason to do it this way (Actually I'm curious why you
do it this way).

Why don't you do it the 'regular' Rails way with f.error_messages?

Note that this method is not available in Rails 3 anymore. You have to
use the plugin 'dynamic_form' made by the Rails Core Team I guess. For
more information see: https://github.com/joelmoss/dynamic_form

Jeroen van Ingen wrote in post #1018925:

Why don't you do it the 'regular' Rails way with the method
'f.error_messages'?

Note that this method is not available in Rails 3 anymore.
(Actually I'm curious why you
do it this way).

I have an app which I have upgraded to rails3.

Duh.

Neil Bye wrote in post #1018907:

I have an app which I have upgraded to rails3. It is working fine but
for displaying error messages.

Take i.e. new.html.erb. In the model I have several validations that
work, they do not create a user if the entries are wrong. On submission
it dutifully returns to new.html.erb but WITHOUT the nice red error
messages.

I have been using

  <% 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 %>

but that doesn't help.

1) Is that a partial? If so, are you actually including it in a view
somewhere, e.g.

<%= render '...../error_messages' %>

2) Is the partial named correctly, e.g.: '_error_messages.html.erb ?

3) I'm not quite sure what the directory rules are for rails, but is the
partial in a directory that can be accessed by multiple controllers,
e.g. app/views/shared/_error_messages.html.erb?

Of course, you would have to have I think the problem may be deeper. Do you know
what files are used when displaying the errors? Where does the error
variable come from?.

It's 'errors' not 'error', and @user is an ActiveRecord::Base object, so
the errors() method must be defined in that class or one of that class's
parent classes.

Could it be a js problem?

Sure. If your js says to hide any html elememts with
id="error_explanation", then none of the error messages will be visible.

Jeroen van Ingen wrote in post #1018925:

The way you want to display the errors looks cumbersome to me. But maybe
you have a good reason to do it this way (Actually I'm curious why you
do it this way).

http://railscasts.com/episodes/211-validations-in-rails-3

7stud -- wrote in post #1018959:

There are no partials involved I

@user is an ActiveRecord::Base object, so
the errors() method must be defined in that class or one of that class's
parent classes (or in a module that one of those classes includes).

Ok I gather that to use p.errors.full_messages I need

def initialize
    @errors = ActiveModel::Errors.new(self)
end

From http://api.rubyonrails.org/classes/ActiveModel/Errors.html

But if I do it throws up 'You have a nil object when you didn't expect
it!' in relation to

<%= form_for @user, :html => {:multipart => true} do |f| %>

I have attached the relevant files.

Am I on the right track now?

Attachments:
http://www.ruby-forum.com/attachment/6561/new.html.erb
http://www.ruby-forum.com/attachment/6562/user.rb

An age-old problem:
http://blog.dalethatcher.com/2008/03/rails-dont-override-initialize-on.html

I now have <%= User.new.errors.full_messages %> in my view and this

extend ActiveModel::Naming

  def after_initialize
    @errors = ActiveModel::Errors.new(self)
  end

  attr_accessor :login
  attr_reader :errors

  def validate!
    errors.add(:login, "can not be nil") if login == nil
  end

  def read_attribute_for_validation(attr)
    send(attr)
  end

  def User.human_attribute_login(attr, options = {})
    attr
  end

  def User.lookup_ancestors
    [self]
  end

In my model

It doesn't throw up any errors but neither does it work.

Where am I going wrong now?

A new User object's errors are going to be empty unless validation has
been run (with .valid? or .save, etc).

Going back to your original problem (I've just looked up your first
message), you seem to be trying to add .errors to a model inheriting
from ActiveRecord::Base - but that already *has* a .errors method. So
your problem is likely to be elsewhere.

In the view that's not working, before you conditionally check for
errors, can you inspect the error object, and see what it is. Also,
have you tried using the "standard" error_messages_for method - just
to check?

M

I don't want to use the old method I am trying to learn the new method.
I have attached the latest versions of the relevant files.

The call <%= @user.errors.full_messages %> gives []
The call <%= User.new.errors %> gives {} as might be expected.

What am I doing wrong now?

Attachments:
http://www.ruby-forum.com/attachment/6563/user.rb
http://www.ruby-forum.com/attachment/6564/new.html.erb

Neil Bye wrote in post #1019016:

7stud -- wrote in post #1018959:

There are no partials involved I

@user is an ActiveRecord::Base object, so
the errors() method must be defined in that class or one of that class's
parent classes (or in a module that one of those classes includes).

Ok I gather that to use 'u.errors.full_messages I need

def initialize
    @errors = ActiveModel::Errors.new(self)
end

in user.rb

From http://api.rubyonrails.org/classes/ActiveModel/Errors.html

No. errors() works automatically in rails3. According to the docs,
errors() returns an object of class ActiveModel::Errors:

http://ar.rubyonrails.org/classes/ActiveRecord/Errors.html

I think the docs you looked at tell you how to provide your model with
an OrderedHash that contains the errors.

I don't know if this could be causing your lack of errors:

validates_presence_of :email

In rails 3 that is written like this:

validates :email, :presence => true

7stud -- wrote in post #1019116:

I think the docs you looked at tell you how to provide your model with
an OrderedHash that contains the errors.

Or maybe those docs are for adding error facilities to classes that do
not inherit from ActiveRecord::Base.

Thanks that sorted it. I built a new class within my app where errors
worked.
It was differences in the controller code that did it.

Cheers everybody