# Why!? A strange instance variable in action and view

**URL:** <https://www.ruby-forum.com/t/why-a-strange-instance-variable-in-action-and-view/171440>\
**Category:** Rails\
**Created:** [July 2, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/why-a-strange-instance-variable-in-action-and-view/171440 "2009-07-02T14:14:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dandan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandan](https://www.ruby-forum.com/u/dandan)\
**Post date:** [July 2, 2009, 2:14pm UTC](https://www.ruby-forum.com/t/why-a-strange-instance-variable-in-action-and-view/171440/1 "2009-07-02T14:14:55Z")

</div>

I have a controller promos. When the edit action is called the  
following error is given -  
Called id for nil, which would mistakenly be 4 – if you really wanted  
the id of nil, use object\_id

For simplicity the controller and view looks like this:

## promos\_controller.rb

## def edit @promo = Promo.find(params[:id]) end

## edit.html.erb

## \<%= @promo.id %\>

However when I change the instance variable to say @home, or anything  
else the view correctly displays the results.

Could anyone explain why it wouldn’t work for @promo?

---

<div class="post-metadata">

**Author:** ![dandan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandan](https://www.ruby-forum.com/u/dandan)\
**Post date:** [July 2, 2009, 2:29pm UTC](https://www.ruby-forum.com/t/why-a-strange-instance-variable-in-action-and-view/171440/2 "2009-07-02T14:29:32Z")

</div>

On Thu, Jul 2, 2009 at 1:47 AM, Dandan[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def edit
> 
> Could anyone explain why it wouldn’t work for @promo?

It’s not the instance variable name, it’s that that instance variable  
has a nil value.

What was the id parameter in the request? I suspect that the find  
failed to find a record with that id, which would cause it to return  
nil.

Your controller should be doing something like:

## def edit unless(@promo = Promo.find(params[:id])) # There was no promo with that id # Perform some appropriate error action # Which might involve setting the flash with an error msg # and redirecting somewhere end end

Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![dandan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/a87d85/32.png) [@dandan](https://www.ruby-forum.com/u/dandan)\
**Post date:** [July 2, 2009, 10:42pm UTC](https://www.ruby-forum.com/t/why-a-strange-instance-variable-in-action-and-view/171440/3 "2009-07-02T22:42:43Z")

</div>

Quoting D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):  
[snip]

> ## promos\_controller.rb
> 
> ## def edit @promo = Promo.find(params[:id]) end
> 
> ## edit.html.erb
> 
> ## \<%= @promo.id %\>

I find that model.id is troublesome. model[:id] works quite reliably  
for me.

HTH,  
Jeffrey
