Errors.add_to_base


#1

What are the limitations on using:

errors.add_to_base

to display errors in views?

I have tried for days to add errors from my object.rb and they never get
displayed.

class Keyword < ActiveRecord::Base
validates_presence_of(:name, :message => “Name is required.”)
validates_uniqueness_of(:name, :message => “This name is already in
use. Please try another.”)

# counter_cache should be set to true, but it is broken!
acts_as_tree :order => "name", :counter_cache => false

def set_parent(parent_name)
  if (parent_name == '')
WHY IS THIS NOT WORKING ???
    errors.add_to_base("Name of parent cannot be empty")
  elsif (self.name == parent_name)
    errors.add_to_base("Parent invalid: a keyword cannot be a parent 

of itself!")
else
@parent = Keyword.find(:first, :conditions => [ “name = ?”,
parent_name ])
if @parent
self.parent_id = @parent.id
# this is necessary as long as the counter_cache of
acts_as_tree is broken
@parent.update_children_count
else
errors.add_to_base("Parent invalid! No keyword found with
name: " + parent_name)
end
end
end

The controller has:

@keyword.set_parent(parent_name)

The view has:

<%= error_messages_for ‘keyword’ %>

the messages from the validates_ methods do get displayed. But why do
the ones from set_parent(parent_name) never appear even if set_parent
gets executed and I know?


#2

I would think that your problem lies with the logic and not
‘add_to_base’.

Try putting some debug messages via logger (or anything else) in your
code to see if that code block is ever getting executed. Please a
debug statement before and after your ‘add_to_base’ call, and let me
know if those ever even fire.

On 4/7/06, Isabelle removed_email_address@domain.invalid wrote:

WHY IS THIS NOT WORKING ???

acts_as_tree is broken


Posted via http://www.ruby-forum.com/.


Rails mailing list
removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails


http://www.michaelgorsuch.org


#3

Michael G. wrote:

I would think that your problem lies with the logic and not
‘add_to_base’.

Try putting some debug messages via logger (or anything else) in your
code to see if that code block is ever getting executed. Please a
debug statement before and after your ‘add_to_base’ call, and let me
know if those ever even fire.

the event gets fired. I use logger.debug, raise errors, they all are
fine. The problem is that the errors object simply is not passed on.

I would very much appreciate it if somebody would post a model class
that contains

errors.add_to_base

in methods other than validates_ or validate_ and tell me that it works!

many thanks for your help

Isabelle

PS: I have given up putting any validation in the model for
parent/children objects. This means duplication of code for
update/create.

the following is not elegant, but it works:

#keywords_controller.rb

def create
# keep entered parameters in memory so the user
# does not have to re-enter everything if validation fails.
@keyword = Keyword.new(params[:keyword])
parent_name = params[:parent][:name].strip

if (parent_name != '' )
  # reject auto-reference
  if parent_name == params[:keyword][:name]
    flash[:notice] = 'Parent invalid: a keyword cannot be a parent 

of itself!’
render :action => ‘new’
return false
else
@parent = Keyword.find(:first, :conditions => [ “name = ?”,
parent_name ])
if @parent
params[:keyword][:parent_id] = @parent.id
@keyword = Keyword.new(params[:keyword])
else
flash[:notice] = 'Parent invalid! No keyword found with name:
’ + parent_name
render :action => ‘new’
return false
end # if @parent
end # if parent_name
else
# Force all new keywords to be attached to a parent
flash[:notice] = ‘You must specify a parent for this keyword.’
render :action => ‘new’
return false
end
if @keyword.save
# this is necessary as long as the counter_cache of acts_as_tree
is broken
@parent.update_children_count
redirect_to :action => ‘show’, :id => @keyword
return
end
end

I am sure there are better ways but I wasn’t able to find anything on
google. Any hints greatly appreciated!