# Problems with error\_messages\_for

**URL:** <https://www.ruby-forum.com/t/problems-with-error-messages-for/86143>\
**Category:** Rails\
**Created:** [February 20, 2007, 6:34am UTC](https://www.ruby-forum.com/t/problems-with-error-messages-for/86143 "2007-02-20T06:34:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jake](https://www.ruby-forum.com/u/jake)\
**Post date:** [February 20, 2007, 6:34am UTC](https://www.ruby-forum.com/t/problems-with-error-messages-for/86143/1 "2007-02-20T06:34:34Z")

</div>

I can’t get my error messages to display. Maybe I don’t yet understand  
page life cycle or something? ROR is an elegant solution but the  
transition from [ASP.net](http://ASP.net) has been painful to say the least. Code below  
any help would be greatly appreciated.

\<% form\_for :inventory\_line\_item, :url =\> { :action  
=\> :create\_inventory\_item, :id =\>@product } do |form| %\>  
\<%= error\_messages\_for ‘inventory\_line\_item’ %\>

1. Quantity \<%= form.text\_field 'quantity' %\> 

Model

class InventoryLineItem \< ActiveRecord::Base  
belongs\_to :products

validates\_presence\_of :date\_added, :added\_by, :quantity, :unit\_case,  
:case\_price  
validates\_numericality\_of :case\_price, :message=\>“Price must be a  
valid decimal number”

protected  
def validate  
errors.add(:case\_price, “should be at lease 0.01”) if  
case\_price.nil? || case\_price \< 0.01  
end

end

controller

def inventory  
@product = Product.find(params[:id])  
@inventory\_line\_item = InventoryLineItem.new  
end

def create\_inventory\_item  
member = Member.find(session[:member\_id])

```
@product = Product.find(params[:id])

```

line\_item =  
InventoryLineItem.new(params[:inventory\_line\_item])  
line\_item.date\_added = Date.today  
line\_item.added\_by = member.name  
@product.inventory\_line\_items \<\< line\_item

```
if @product.save
  flash[:notice] = 'Inventory successfully added'
  redirect_to :action => 'inventory', :id => @product
end
  redirect_to :action => 'inventory', :id => @product

```

end

---

<div class="post-metadata">

**Author:** ![jake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jake](https://www.ruby-forum.com/u/jake)\
**Post date:** [February 20, 2007, 6:39am UTC](https://www.ruby-forum.com/t/problems-with-error-messages-for/86143/2 "2007-02-20T06:39:34Z")

</div>

On 2/20/07, jake [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I can’t get my error messages to display. Maybe I don’t yet understand  
> page life cycle or something? ROR is an elegant solution but the  
> transition from [ASP.net](http://ASP.net) has been painful to say the least. Code below  
> any help would be greatly appreciated.
> 
> \<% form\_for :inventory\_line\_item, :url =\> { :action  
> =\> :create\_inventory\_item, :id =\>@product } do |form| %\>  
> \<%= error\_messages\_for ‘inventory\_line\_item’ %\>

This line requires that you have the instance variable  
@inventory\_line\_item  
set and containing the object that you are interested in.

\> \> end \> \> if @product.save \> flash[:notice] = 'Inventory successfully added' \> redirect\_to :action =\> 'inventory', :id =\> @product \> end \> redirect\_to :action =\> 'inventory', :id =\> @product \> end

line\_item in the create action should be  
@inventory\_line\_item

This should then see it

---

<div class="post-metadata">

**Author:** ![jake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jake](https://www.ruby-forum.com/u/jake)\
**Post date:** [February 20, 2007, 6:52am UTC](https://www.ruby-forum.com/t/problems-with-error-messages-for/86143/3 "2007-02-20T06:52:35Z")

</div>

I follow your logic, if I am using the instance variable it will not  
get passed back to the page.

I made the following change but am still not getting any error  
messaging? Could it be because I am using @product.save with \<%=  
error\_messages\_for ‘inventory\_line\_item’ %\> or could it be that I am  
instantiating and new inventory\_line\_item in inventory? Maybe the  
errors are not bubbling up or something. I am confused for sure on  
this one.

#inventory controls  
def inventory  
@product = Product.find(params[:id])  
@inventory\_line\_item = InventoryLineItem.new  
end

def create\_inventory\_item  
member = Member.find(session[:member\_id])

```
@product = Product.find(params[:id])

```

@inventory\_line\_item =  
InventoryLineItem.new(params[:inventory\_line\_item])  
@inventory\_line\_item.date\_added = Date.today  
@inventory\_line\_item.added\_by = member.name  
@product.inventory\_line\_items \<\< @inventory\_line\_item  
if @product.save  
flash[:notice] = ‘Inventory successfully added’  
end  
redirect\_to :action =\> ‘inventory’, :id =\> @product  
end

---

<div class="post-metadata">

**Author:** ![jake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jake](https://www.ruby-forum.com/u/jake)\
**Post date:** [February 20, 2007, 8:10am UTC](https://www.ruby-forum.com/t/problems-with-error-messages-for/86143/4 "2007-02-20T08:10:44Z")

</div>

On 2/20/07, jake [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I follow your logic, if I am using the instance variable it will not  
> get passed back to the page.

That’s not quite it. The error\_messages\_for method looks for an  
instance variable with the name given in the argument.

In the docs

Returns a string with a div containing all of the error messages for  
the objects located as instance variables by the names given. If more  
than one object is specified, the errors for the objects are displayed  
in the order that the object names are provided.

> **[ActionView::Helpers::FormTagHelper](https://api.rubyonrails.org/classes/ActionView/Helpers/FormTagHelper.html#M000603)**
>
> Provides a number of methods for creating form tags that don't rely on an Active Record object assigned to the template like FormHelper does. Instead, you provide the names and values manually.

> I made the following change but am still not getting any error  
> messaging? Could it be because I am using @product.save with \<%=  
> error\_messages\_for ‘inventory\_line\_item’ %\> or could it be that I am  
> instantiating and new inventory\_line\_item in inventory? Maybe the  
> errors are not bubbling up or something. I am confused for sure on  
> this one.

I believe the problem is that you are not saving @inventory\_line\_item  
or at least calling the validation, and it is not saving as part of  
the @product.save.

You will need to at least run the valid? method on the  
@inventory\_line\_item or make sure that an attempt has been made to  
save it.

Hope that helps.

---

<div class="post-metadata">

**Author:** ![jake](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/f475e1/32.png) [@jake](https://www.ruby-forum.com/u/jake)\
**Post date:** [February 20, 2007, 7:52pm UTC](https://www.ruby-forum.com/t/problems-with-error-messages-for/86143/5 "2007-02-20T19:52:38Z")

</div>

Thanks Daniel,

Below is the code that fixed this issue. I had to check the valid  
method first like you said and the key to making the validation  
display was using the render action: =\> to redisplay the page like  
this. When you redirect\_to do you loose the state of the  
error\_message\_for? I will need to read up on that. Thanks againf for  
your help.

```
 @inventory_line_item.added_by = member.name
  if @inventory_line_item.valid?
   @product.inventory_line_items << @inventory_line_item
    if @product.save
       flash[:notice] = 'Inventory successfully added'
     redirect_to :action => 'list'
     end
else
  render :action => :inventory
end
```
