# Nil.errors and error\_messages\_for

**URL:** <https://www.ruby-forum.com/t/nil-errors-and-error-messages-for/163919>\
**Category:** Rails\
**Created:** [March 26, 2009, 5:23am UTC](https://www.ruby-forum.com/t/nil-errors-and-error-messages-for/163919 "2009-03-26T05:23:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bongoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/67e7ee/32.png) [@bongoman](https://www.ruby-forum.com/u/bongoman)\
**Post date:** [March 26, 2009, 5:23am UTC](https://www.ruby-forum.com/t/nil-errors-and-error-messages-for/163919/1 "2009-03-26T05:23:08Z")

</div>

Hi there

I’m wanting to re-render my index action in the event of an object not  
getting saved; I use render not redirect so as to preserve the  
validation errors.

I’m using the error\_messages\_for helper in my view but I get a  
nil.errors error when I attempt to display the view.

I can avoid this error by setting up @discount = Discount.new in my  
index action, but this obliterates the instance variable with the  
attached error messages.

I’m chasing my tail on this and any clues would be appreciated.

---

<div class="post-metadata">

**Author:** ![bongoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/67e7ee/32.png) [@bongoman](https://www.ruby-forum.com/u/bongoman)\
**Post date:** [March 26, 2009, 5:37am UTC](https://www.ruby-forum.com/t/nil-errors-and-error-messages-for/163919/2 "2009-03-26T05:37:45Z")

</div>

bongoman wrote:

> I’m chasing my tail on this and any clues would be appreciated.

Hear galloping think horses not zebras, here…

Your action goes like…

@discount = Discount.new(params[:discount]) # warning use  
attr\_protected!  
unless @discount.save  
render :action =\> ‘index’  
end

So… uh… @discount indeed contains a stuffed Discount instance with  
populated  
errors… so what’s the problem??

So post your actual action instead of making us guess? (-8

---

<div class="post-metadata">

**Author:** ![bongoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/67e7ee/32.png) [@bongoman](https://www.ruby-forum.com/u/bongoman)\
**Post date:** [March 26, 2009, 5:57am UTC](https://www.ruby-forum.com/t/nil-errors-and-error-messages-for/163919/3 "2009-03-26T05:57:37Z")

</div>

> So… uh… @discount indeed contains a stuffed Discount instance  
> with populated  
> errors… so what’s the problem??

Well it only does when the index template is rendered AFTER validation  
fails. The same action gets invoked prior to the form submission at  
which point in time there is no @discount variable, hence the  
nil.errors

> So post your actual action instead of making us guess? (-8

Here we go:

```
@discount = Discount.find(params[:id])
if @discount.update_attributes(params[:discount])
  flash[:notice_good] = "Discounts have been updated"
  redirect_to :action=> 'index'
else
  index # I need to do this in order to get my instance variables

```

populated for the index view  
render :action =\> ‘index’  
end

---

<div class="post-metadata">

**Author:** ![bongoman](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/67e7ee/32.png) [@bongoman](https://www.ruby-forum.com/u/bongoman)\
**Post date:** [March 26, 2009, 6:12am UTC](https://www.ruby-forum.com/t/nil-errors-and-error-messages-for/163919/4 "2009-03-26T06:12:49Z")

</div>

> Here we go:
> 
> ```
> @discount = Discount.find(params[:id])
> if @discount.update_attributes(params[:discount])
> flash[:notice_good] = "Discounts have been updated"
> redirect_to :action=> 'index'
> else
> index # I need to do this in order to get my instance variables
> 
> ```
> 
> populated for the index view

and … index re-populates @discount?

put this inside the index:

@discount ||= Discount.new
