Error with errors

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


<%= pluralize(@user.errors.count, “error”) %> prohibited this
user from being saved:

  <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 the method
‘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: GitHub - joelmoss/dynamic_form: Helpers to deal with your model backed forms in Rails3

Neil B. 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? %>


<%= pluralize(@user.errors.count, “error”) %> prohibited this
user from being saved:

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

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

  2. 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 it’s not a variable–it’s a method. In
ruby,
you access instance variables by calling methods, so everything after
an object’s name is a method call. Just looking at the code, there is
no way to know if there is an instance variable called @errors that
errors() returns or whether errors() is a method that does something
else.

@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).

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:

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.

I have an app which I have upgraded to rails3.

(Actually I’m curious why you
do it this way).

Seems pretty clear.

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).

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 ActiveModel::Errors

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?

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?

On 29 August 2011 11:33, Neil B. [email protected] wrote:

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

An age-old problem:

On 29 August 2011 12:54, Neil B. [email protected] wrote:

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

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?

Neil B. 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 ActiveModel::Errors

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

I would create a new rails app to see if you can get errors() to work.
Just create a model like this:

$ rails generate model User name:string email:string

Then add a simple validation:

class User < ActiveRecord::Base
attr_accessible :name, :email

validates :name, :presence => true

end

Then test it out in the console:

$ rails console --sandbox

ruby-1.9.2-p180 :002 > user = User.new(:name => ‘’, :email =>
[email protected]’)
=> #<User id: nil, name: “”, email: “[email protected]”, created_at: nil,
updated_at: nil>
ruby-1.9.2-p180 :003 > user.save
=> false
ruby-1.9.2-p180 :004 > user.errors.full_messages
=> [“Name can’t be blank”]
ruby-1.9.2-p180 :005 >

You might even want to try that in your current app.

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