# Error\_messages\_for does not display the error

**URL:** <https://www.ruby-forum.com/t/error-messages-for-does-not-display-the-error/164579>\
**Category:** Rails\
**Created:** [April 2, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/error-messages-for-does-not-display-the-error/164579 "2009-04-02T18:54:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [April 2, 2009, 6:54pm UTC](https://www.ruby-forum.com/t/error-messages-for-does-not-display-the-error/164579/1 "2009-04-02T18:54:50Z")

</div>

Hi all

I’ve approached Rails since a couple of months to develop a quick  
application for my company. Fantastic framework. As every noob, I do  
have some gaps to cover. The one which is giving me a little  
frustration, generated by my lack of knowledge is as follows.

I need to make sure the region object is not deleted if there are  
countries associated with it. I solved this by putting a  
before\_destroy methon in the model, as follows:

class Region \< ActiveRecord::Base  
has\_many :countries

validates\_presence\_of :name  
validates\_uniqueness\_of :name

def before\_destroy  
unless countries.count == 0  
errors.add\_to\_base “Cannot delete a region with countries  
associated”  
return false  
end  
end  
end

This prevents the deletion if countries are associated to it. By  
adding the error to the errors collection of the region object, I  
expected to receive a message on the page by adding the follwing in  
the application layout

—cut—  
\<%= error\_messages\_for :region %\>  
\<%= yield :layout %\>  
—cut—

actually the error\_messages\_for is much longer as it lists all the  
objects. The tag works for input forms, but in this specific scenario  
no error message is displayed.

The error is however populated, I verified it with the logger.info in  
the various steps.

After crushing my head trying to figure out why, I gave up the old  
way, finding another solution. But I do not like it because ruby and  
rails are very elegant in their syntax. Here is what I did: in the  
controller I modified the destroy method like this

def destroy  
@region = Region.find(params[:id])  
@region.destroy

```
@region.errors.each_full{|msg| flash[:error] = msg } unless

```

@region.errors.count == 0

```
respond_to do |format|
  format.html { redirect_to(regions_url) }
  format.xml { head :ok }
end

```

end

I know, it’s an horror, I can name many resons why including the fact  
that only the last error is displayed, but could not figure out a  
better way. I’m pretty sure that the solution is right in front of me  
but cannot find it…

Any suggestion very welcome.

Thanks

---

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [April 2, 2009, 11:33pm UTC](https://www.ruby-forum.com/t/error-messages-for-does-not-display-the-error/164579/2 "2009-04-02T23:33:28Z")

</div>

This might help you out…

[http://api.rubyonrails.org/classes/ActionController/Flash.html](http://api.rubyonrails.org/classes/ActionController/Flash.html)

Although it’s not immediately apparent from the rdocs, you can flash  
[:error] in addition to the documented flash[:notify] - think of them  
as “alerts” and “alarms”. If you carry the error/notify distinction  
into the erb with:

```
 <% if flash[:notice] %>
   <div class="notice"><%= flash[:notice] %></div>
 <% end %>
  <% if flash[:error] %>
   <div class="error"><%= flash[:error] %></div>
 <% end %>

```

you can use the class to provide css color decoration (notice \<=\>  
yellow, error \<=\> red)

Just be aware that the flash is a temporary buffer - the text is  
volatile by default - and refreshing the window will erase the  
message.

---

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [April 2, 2009, 11:35pm UTC](https://www.ruby-forum.com/t/error-messages-for-does-not-display-the-error/164579/3 "2009-04-02T23:35:15Z")

</div>

On Apr 2, 10:32 pm, Rick [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This might help you out…
> 
> [ActionController::Flash](http://api.rubyonrails.org/classes/ActionController/Flash.html)
> 
> Although it’s not immediately apparent from the rdocs, you can flash  
> [:error] in addition to the documented flash[:notify] - think of them  
> as “alerts” and “alarms”. If you carry the error/notify distinction  
> into the erb with:

To extend on that, flash is just a hash (which happens to be stored in  
the session and which rails clears out appropriately) - you can store  
anything you want in it, for any key you want.

Fred

---

<div class="post-metadata">

**Author:** ![steve](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f19dbf/32.png) [@steve](https://www.ruby-forum.com/u/steve)\
**Post date:** [April 3, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/error-messages-for-does-not-display-the-error/164579/4 "2009-04-03T14:14:30Z")

</div>

Hi all

I found the solution to my problems…

My mistake (of course because of ignorance) was to look in the wrong  
place. The error populated in the model.errors hash are rendered by  
the error\_messages\_for method only if the render method in the  
ActionController is invoked.

Therefore the code should look like this.

The model:

class Region \< ActiveRecord::Base  
has\_many :countries

validates\_presence\_of :name  
validates\_uniqueness\_of :name

def before\_destroy  
unless countries.count == 0  
errors.add\_to\_base “Cannot delete a region with countries  
associated”  
return false  
end  
end  
end

The method destroy in the controller

def destroy  
@region = Region.find(params[:id])

```
respond_to do |format|
  if @region.destroy
    format.html { redirect_to(regions_url) }
    format.xml { head :ok }
  else
    format.html { render :action => "show" }
    format.xml { render :xml => @region.errors, :status

```

=\> :unprocessable\_entity }  
end  
end  
end

Et voilà, here is the error displayed on screen…

Steve
