# NilClass passed to partial view

**URL:** <https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342>\
**Category:** Rails\
**Created:** [May 10, 2010, 2:49pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342 "2010-05-10T14:49:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [May 10, 2010, 2:49pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342/1 "2010-05-10T14:49:11Z")

</div>

## Hi, I have a simple program that tries to do something really basic. First, I have a company Model :

class Company \< ActiveRecord::Base  
has\_one :billingAddress, :foreign\_key=\>“id”, :class\_name=\>“Address”  
end

## Then I have the address Model :

class Address\< ActiveRecord::Base  
belongs\_to :company  
end

I’ve created manually an entry in the DB for Address and Company and  
I’ve linked them (Company table has a “billingAddress\_id” row)

In my views “company/\_form”(scaffold generated), I want to display the  
address form. So I added (following Rails Guides) the following line to  
my view :

\<%= render :partial =\> “addresses/edit”, :locals =\> { :shippingAddress  
=\> @address} %\>  
It keeps saying : "undefined method `model\_name’ for NilClass:Class "  
tried this :  
\<%= render :partial =\> “addresses/edit”, :locals =\> {  
@company.shippingAddress =\> @address} %\>  
with the same result

I’ve tried with :  
\<%= render :partial =\> “addresses/form”, :object =\> @billingAddress%\>  
and I got the same result (this is normal I think since I don’t have a  
billingAddress object but a Company object.

Finally, I’ve tried :  
\<%= :partial =\> “addresses/form”, :object =\> @company.shippingAddress %\>  
and the error I got was :  
syntax error, unexpected tASSOC, expecting ')

When I load the Company with rails console, it loads perfectly. However  
I cannot manage to call the aggregated object view since the name of the  
variable is not the same name as the class name… at least, that what I  
think it is 🙂

Can someone help me to figure this out ? I thought that this will be a  
simple task but it figures that it is not…

Thank you very much for your time !

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [May 10, 2010, 2:58pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342/2 "2010-05-10T14:58:24Z")

</div>

On 10 May 2010 13:49, Greg R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> class Address\< ActiveRecord::Base  
> \<%= render :partial =\> “addresses/edit”, :locals =\> { :shippingAddress  
> =\> @address} %\>  
> It keeps saying : "undefined method `model\_name’ for NilClass:Class "  
> tried this :

In the partial have you got something.model\_name? If so then  
‘something’ is nil.

In the traceback when you get the error message you should be able to  
see the line that is failing.

Colin

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [May 10, 2010, 3:04pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342/3 "2010-05-10T15:04:14Z")

</div>

Colin L. wrote:

> On 10 May 2010 13:49, Greg R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > class Address\< ActiveRecord::Base  
> > \<%= render :partial =\> “addresses/edit”, :locals =\> { :shippingAddress  
> > =\> @address} %\>  
> > It keeps saying : "undefined method `model\_name’ for NilClass:Class "  
> > tried this :
> 
> In the partial have you got something.model\_name? If so then  
> ‘something’ is nil.
> 
> In the traceback when you get the error message you should be able to  
> see the line that is failing.
> 
> Colin  
> Thanks Colin for the fast answer :

In the partial I have the “standard” scaffolded code for my Address  
class

So it’s a “form\_for @address” just like Rails 3 is generates…

so I don’t understand why I can go to /addresses/1 and see the form but  
I cant view the same info via a partial.

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [May 10, 2010, 3:28pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342/4 "2010-05-10T15:28:16Z")

</div>

On May 10, 2:49 pm, Greg R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## Hi, I have a simple program that tries to do something really basic. First, I have a company Model :
> 
> class Company \< ActiveRecord::Base  
> has\_one :billingAddress, :foreign\_key=\>“id”, :class\_name=\>“Address”  
> end

I think your problem is in :foreign\_key=\>“id” part. This means that  
foreign key to address is named ‘id’ which is actually id column for  
Company model. Remove this part if your foreign key column is  
‘address\_id’ or use real column name, i.e. ‘billing\_address\_id’  
otherwise

Regards,  
Bosko

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [May 10, 2010, 3:18pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342/5 "2010-05-10T15:18:19Z")

</div>

On 10 May 2010 14:04, Greg R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > In the traceback when you get the error message you should be able to  
> > see the line that is failing.
> > 
> > Colin  
> > Thanks Colin for the fast answer :
> 
> In the partial I have the “standard” scaffolded code for my Address  
> class
> 
> So it’s a “form\_for @address” just like Rails 3 is generates…

You did not say you are using Rails 3 (unless I missed it). I have no  
experience of Rails 3, which is still under development. You did not  
answer the question as to whether you are accessing  
something.model\_name, nor whether the trace shows the failing line.

You do not specify your knowledge level, if you are just starting I  
would not recommend rails 3. You will never know whether a problem is  
down to your experience or a problem with rails itself.

Colin

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [May 10, 2010, 3:31pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342/6 "2010-05-10T15:31:39Z")

</div>

> You did not say you are using Rails 3 (unless I missed it). I have no  
> experience of Rails 3, which is still under development. You did not  
> answer the question as to whether you are accessing  
> something.model\_name, nor whether the trace shows the failing line.
> 
> You do not specify your knowledge level, if you are just starting I  
> would not recommend rails 3. You will never know whether a problem is  
> down to your experience or a problem with rails itself.
> 
> Colin

Hi Colin,  
Yes I’m using Rails 3. My knowledge level is … well, begginer since  
I’m not able to make this thing works 🙂 My years of experience are  
with PHP and .Net. I’m trying Rails because it looks interesting and  
promising (once I’ll get a solid grip on basic things like this!)

Here is my partial view for the Address class.  
1\<%= form\_for(@address) do |f| %\>  
2

  
3 \<%= f.label :city %\>  
  
4 \<%= f.text\_field :city %\>  
5 
  
6  
7\<% end %\>

The failling line is #3. I’m able to load the partial if I put in it a  
string and no code (like "

# TEST
"). I’m not sure that I  
understand what you meant by “something.model\_name” … As for Rails 3,  
they said on guides.rails.info :

“But if youâ€™re starting development on a new application and you donâ€™t  
mind getting wind in your hair, please do jump on board!” I’m starting  
a new app and I’m eager to learn so that’s what I’ve done… is it a  
choice that bad ?

Thank you for you replies !  
Greg

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [May 10, 2010, 3:56pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342/7 "2010-05-10T15:56:22Z")

</div>

On 10 May 2010 14:31, Greg R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 4 \<%= f.text\_field :city %\>  
> 5   
> 6  
> 7\<% end %\>
> 
> The failling line is #3. I’m able to load the partial if I put in it a  
> string and no code (like “
> 
> # TEST
> ”). I’m not sure that I  
> understand what you meant by “something.model\_name”

I think Bosko may well have the answer. I was assuming that one of  
your models had a file model\_name but I see now that this is the rails  
model it is talking about.

… As for Rails 3,

> they said on guides.rails.info :
> 
> “But if you’re starting development on a new application and you don’t  
> mind getting wind in your hair, please do jump on board!” I’m starting  
> a new app and I’m eager to learn so that’s what I’ve done… is it a  
> choice that bad ?

I think this may have been intended for experienced users starting a  
new app rather than beginners. You will not find many tutorials and  
guides for rails 3 yet. I would consider going back to 2.3.x and  
starting with the guides at [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)

Colin

---

<div class="post-metadata">

**Author:** ![rubygreg](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/53a042/32.png) [@rubygreg](https://www.ruby-forum.com/u/rubygreg)\
**Post date:** [May 10, 2010, 4:32pm UTC](https://www.ruby-forum.com/t/nilclass-passed-to-partial-view/188342/8 "2010-05-10T16:32:37Z")

</div>

@Bosko : I’ll try your suggestion tonight and keeps you posted.

@Collin : Thanks for your time… I’ll check for rails 2.3.x and if I’m  
not able to make this work, I may roll back

Thank you guys for your help !
