# NoMethodError with unexpected nil object in ERB

**URL:** <https://www.ruby-forum.com/t/nomethoderror-with-unexpected-nil-object-in-erb/174503>\
**Category:** Rails\
**Created:** [August 27, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/nomethoderror-with-unexpected-nil-object-in-erb/174503 "2009-08-27T18:32:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ryokamiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3da27b/32.png) [@ryokamiya](https://www.ruby-forum.com/u/ryokamiya)\
**Post date:** [August 27, 2009, 6:32pm UTC](https://www.ruby-forum.com/t/nomethoderror-with-unexpected-nil-object-in-erb/174503/1 "2009-08-27T18:32:03Z")

</div>

I have created original method and trying to pass newly instantiated  
object to ERB file, but encountering NoMethodError in ERB file with the  
following error message.

* * *

NoMethodError in Products#lookup

Showing app/views/products/lookup.html.erb where line #3 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of ActiveRecord::Base.  
The error occurred while evaluating nil.[]  
Extracted source (around line #3):

## 1:

# Add product
  
2:  
3: \<% form\_for(@product) do |f| %\>  
4: \<%= f.error\_messages %\>  
5:  
6: 

## Source Code products\_controller.rb

# GET /products/lookup

# GET /products/lookup.xml

## def lookup result = Amazon::Ecs.item\_lookup(params[:id]).first\_item @product = AmazonProduct.new( result.get(‘asin’), result.get(‘itemattributes/manufacturer’), result.get(‘title’), “”, result.get(‘detailpageurl’), “” ) end end

## lookup.html.erb

# Add product

\<% form\_for(@product) do |f| %\>  
\<%= f.error\_messages %\>

## 

\<%= f.label :id %\>: \<%= @product.amazon\_asin %\>

When viewing @product variable by running this flow in Aptana debug mode  
and stop it at erb source code, @product seems to have information and  
not nil (See attached). I was trying to copy standard rails  
controller/erb approach for other method, but it’s not working well for  
this. May I have any advice? Thank you.

---

<div class="post-metadata">

**Author:** ![ryokamiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3da27b/32.png) [@ryokamiya](https://www.ruby-forum.com/u/ryokamiya)\
**Post date:** [August 27, 2009, 11:38pm UTC](https://www.ruby-forum.com/t/nomethoderror-with-unexpected-nil-object-in-erb/174503/2 "2009-08-27T23:38:14Z")

</div>

On Aug 27, 5:32 pm, Ryoichiro K. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> When viewing @product variable by running this flow in Aptana debug mode  
> and stop it at erb source code, @product seems to have information and  
> not nil (See attached). I was trying to copy standard rails  
> controller/erb approach for other method, but it’s not working well for  
> this. May I have any advice? Thank you.

is an instance of AmazonProduct look enough like an ActiveRecord model  
that you can pass it to form\_for like that ?

Fred

---

<div class="post-metadata">

**Author:** ![ryokamiya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/3da27b/32.png) [@ryokamiya](https://www.ruby-forum.com/u/ryokamiya)\
**Post date:** [August 29, 2009, 4:37pm UTC](https://www.ruby-forum.com/t/nomethoderror-with-unexpected-nil-object-in-erb/174503/3 "2009-08-29T16:37:44Z")

</div>

Frederick C. wrote:

> On Aug 27, 5:32ï¿½pm, Ryoichiro K. \<rails-mailing-l…@andreas-  
> [s.net](http://s.net)\> wrote:
> 
> > When viewing @product variable by running this flow in Aptana debug mode  
> > and stop it at erb source code, @product seems to have information and  
> > not nil (See attached). I was trying to copy standard rails  
> > controller/erb approach for other method, but it’s not working well for  
> > this. May I have any advice? Thank you.
> 
> is an instance of AmazonProduct look enough like an ActiveRecord model  
> that you can pass it to form\_for like that ?
> 
> Fred

The AmazonProduct is inheriting the Product class which has been  
scaffolded by rails. Is the class which inheriting the ActiveRecord  
model class also assumed ActiveRecord model class?

Class AmazonProduct \< Product
