# Problems with error\_messages\_for

**URL:** <https://discuss.rubyonrails.org/t/problems-with-error-messages-for/9007>\
**Category:** rubyonrails-talk\
**Created:** [February 20, 2007, 5:33am UTC](https://discuss.rubyonrails.org/t/problems-with-error-messages-for/9007 "2007-02-20T05:33:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jake3](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@jake3](https://discuss.rubyonrails.org/u/jake3)\
**Post date:** [February 20, 2007, 5:33am UTC](https://discuss.rubyonrails.org/t/problems-with-error-messages-for/9007/1 "2007-02-20T05:33:44Z")

</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 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' %\> \<ol\> &nbsp;&nbsp;\<li\> &nbsp;&nbsp;&nbsp;&nbsp;\<label for="inventory\_line\_item\_quantity"\>Quantity\</label\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= form.text\_field 'quantity' %\> &nbsp;&nbsp;\</li\>

Model

class InventoryLineItem \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :products

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

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

end

controller

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

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

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

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

---

<div class="post-metadata">

**Author:** ![Daniel\_N](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@Daniel\_N](https://discuss.rubyonrails.org/u/Daniel_N)\
**Post date:** [February 20, 2007, 5:38am UTC](https://discuss.rubyonrails.org/t/problems-with-error-messages-for/9007/2 "2007-02-20T05:38:28Z")

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

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

> 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](http://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
> 
> ```

line\_item in the create action should be

@inventory\_line\_item

This should then see it

---

<div class="post-metadata">

**Author:** ![jake3](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@jake3](https://discuss.rubyonrails.org/u/jake3)\
**Post date:** [February 20, 2007, 5:51am UTC](https://discuss.rubyonrails.org/t/problems-with-error-messages-for/9007/3 "2007-02-20T05:51:43Z")

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

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

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

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

---

<div class="post-metadata">

**Author:** ![Daniel\_N](https://avatars.discourse-cdn.com/v4/letter/d/ba8739/32.png) [@Daniel\_N](https://discuss.rubyonrails.org/u/Daniel_N)\
**Post date:** [February 20, 2007, 7:10am UTC](https://discuss.rubyonrails.org/t/problems-with-error-messages-for/9007/4 "2007-02-20T07:10:04Z")

</div>

> 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)**
>
> Action View Form Tag Helpers 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.

> 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:** ![jake3](https://avatars.discourse-cdn.com/v4/letter/j/a698b9/32.png) [@jake3](https://discuss.rubyonrails.org/u/jake3)\
**Post date:** [February 20, 2007, 3:57pm UTC](https://discuss.rubyonrails.org/t/problems-with-error-messages-for/9007/5 "2007-02-20T15:57:03Z")

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@inventory\_line\_item.added\_by = member.name &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @inventory\_line\_item.valid? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@product.inventory\_line\_items \<\< @inventory\_line\_item &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if @product.save &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;flash[:notice] = 'Inventory successfully added' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redirect\_to :action =\> 'list' &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render :action =\> :inventory &nbsp;&nbsp;&nbsp;&nbsp;end
