# NoMethodError in Classified#create nil object

**URL:** <https://www.ruby-forum.com/t/nomethoderror-in-classified-create-nil-object/89169>\
**Category:** Rails\
**Created:** [March 18, 2007, 3:41am UTC](https://www.ruby-forum.com/t/nomethoderror-in-classified-create-nil-object/89169 "2007-03-18T03:41:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![riottype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@riottype](https://www.ruby-forum.com/u/riottype)\
**Post date:** [March 18, 2007, 3:41am UTC](https://www.ruby-forum.com/t/nomethoderror-in-classified-create-nil-object/89169/1 "2007-03-18T03:41:59Z")

</div>

I’m following along the tutorials of a book (Rails Solutions: Ruby on  
Rails made easy) and I’ve come to a part that I can’t seem to figure  
out. I’m trying to implement validation for file uploads (make sure  
upload is a picture format).

It works fine until I upload a file that isn’t an image. It should  
report an error message, instead the app crashes and I get a  
NoMethodError.

NoMethodError in Classified#create

Showing app/views/classified/new.rhtml where line #15 raised:

You have a nil object when you didn’t expect it!  
You might have expected an instance of Array.  
The error occured while evaluating nil.inject

12:

Email  
  
13: \<%= text\_field ‘classified’, ‘email’ %\>

  
14: 

Category  
  
15: \<%= collection\_select(:classified, :category\_id, @categories,:id,  
:name) %\>

  
16: 

Picture  
  
17: \<%= file\_field ‘classified’, ‘pictureimg’ %\>

  
18:

I checked my Model classified.rb against the books source code and it  
matches, I also checked my classifieds controller, and new.rhmtl file  
that the error references – yet everything seems to be in order, and  
the error persists.

I’m all out of ideas and could use some help.

---

<div class="post-metadata">

**Author:** ![riottype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@riottype](https://www.ruby-forum.com/u/riottype)\
**Post date:** [March 18, 2007, 7:04am UTC](https://www.ruby-forum.com/t/nomethoderror-in-classified-create-nil-object/89169/2 "2007-03-18T07:04:58Z")

</div>

google rails file colum

---

<div class="post-metadata">

**Author:** ![riottype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@riottype](https://www.ruby-forum.com/u/riottype)\
**Post date:** [March 18, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-classified-create-nil-object/89169/3 "2007-03-18T22:20:38Z")

</div>

Keynan P. wrote:

> google rails file colum

Thanks, I’ve bookmarked this plugin for later use - however, right now  
I’m following along the tutorials of the book and I’d rather fix what I  
have before I get in over my head w/ adding new features.

I’m still trying to decypher this error.

---

<div class="post-metadata">

**Author:** ![riottype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@riottype](https://www.ruby-forum.com/u/riottype)\
**Post date:** [May 2, 2008, 10:52am UTC](https://www.ruby-forum.com/t/nomethoderror-in-classified-create-nil-object/89169/4 "2008-05-02T10:52:51Z")

</div>

As a matter a fact, the error is related to the validation itself. It  
‘crashes’ on the relation category-classified.  
So far I found out that the error is introduced in chapter 5 after  
adding the relation category-classified. It has nothing to do with file  
upload.  
If you remove the validations from the model it all works fine.

So far, the author hasn’t come back with an answer to fix it.  
Ideally, you should be able to trap the errors

Anybody got an idea how to overcome this? APparantly, the Ruby code  
tries to retrieve associated records from a category, without having an  
instantiated classifieds array.

---

<div class="post-metadata">

**Author:** ![riottype](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/8dc957/32.png) [@riottype](https://www.ruby-forum.com/u/riottype)\
**Post date:** [June 27, 2008, 6:41pm UTC](https://www.ruby-forum.com/t/nomethoderror-in-classified-create-nil-object/89169/5 "2008-06-27T18:41:52Z")

</div>

Hi, finally I found the solution for this problem, as Marcel D.  
said, the validation crashes on relation category-classified, defined  
on classified.rb. just change “belongs\_to :category”, after all the  
validations.

I hope it works to you.

PD: Sorry about my english,

On 2 mayo, 03:52, Marcel D. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
