NoMethodError in Classified#create nil object

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.

google rails file colum

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.

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.

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. [email protected]