# Newbie:problem when using validates\*

**URL:** <https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980>\
**Category:** Rails\
**Created:** [May 18, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980 "2006-05-18T17:36:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![antonio\_rodriguez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@antonio\_rodriguez](https://www.ruby-forum.com/u/antonio_rodriguez)\
**Post date:** [May 18, 2006, 5:36pm UTC](https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980/1 "2006-05-18T17:36:41Z")

</div>

Hi,

I have performed the cookbook tutorial. Before tailoring the views and  
controllers, when I put in the model recipe.rb:

validates\_uniqueness\_of :title  
validates\_length\_of :title, :within =\> 1…20

And I tried to introduce some new recipe without the above conditions I  
got the message:

* * *

New recipe  
1 error prohibited this category from being saved

There were problems with the following fields:

```
 * Name is too short (minimum is 1 characters)

```

## Name Back

So, just fine. But after tailoring the views and controller I’m not  
longer able to get this, just this one:

NoMethodError in Recipe#create

Showing app/views/recipe/new.rhtml where line #27 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.each

Extracted source (around line #27):

24:

25: **Categoria:**  
  
26:   
27: \<% @categories.each do |category| %\>  
28:   
29: \<%= category.name %\>  
30:

Why the sentences in the model

Some ideas?

BR

## Antonio

## ===== Por favor, si me mandas correos con copia a varias personas, pon mi dirección de correo en copia oculta (CCO), para evitar que acabe en montones de sitios, eliminando mi privacidad, favoreciendo la propagación de virus y la proliferación del SPAM. Gracias.

# If you send me e-mail which has also been sent to several other people, kindly mark my address as blind-carbon-copy (or BCC), to avoid its distribution, which affects my privacy, increases the likelihood of spreading viruses, and leads to more SPAM. Thanks.

# –

## Por favor, si me mandas correos con copia a varias personas, pon mi dirección de correo en copia oculta (CCO), para evitar que acabe en montones de sitios, eliminando mi privacidad, favoreciendo la propagación de virus y la proliferación del SPAM. Gracias.

# If you send me e-mail which has also been sent to several other people, kindly mark my address as blind-carbon-copy (or BCC), to avoid its distribution, which affects my privacy, increases the likelihood of spreading viruses, and leads to more SPAM. Thanks.

---

<div class="post-metadata">

**Author:** ![antonio\_rodriguez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@antonio\_rodriguez](https://www.ruby-forum.com/u/antonio_rodriguez)\
**Post date:** [May 18, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980/2 "2006-05-18T17:45:00Z")

</div>

> NoMethodError in Recipe#create
> 
> Showing app/views/recipe/new.rhtml where line #27 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.each
> 
> Extracted source (around line #27):
> 
> 24:
> 
> 25: **Categoria:**  
>   
> 26:   
> 27: \<% @categories.each do |category| %\>  
> 28:   
> 29: \<%= category.name %\>  
> 30:
> 
> Why the sentences in the model
> 
> Some ideas?
> 
> BR
> 
> Antonio

Just to be sure… you did define @categories in your controller right?  
something like…

```
 def new
    @categories = Catgory.find(:all)
 end

```

if you didn’t then you would most definately get that error.

jon

---

<div class="post-metadata">

**Author:** ![antonio\_rodriguez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@antonio\_rodriguez](https://www.ruby-forum.com/u/antonio_rodriguez)\
**Post date:** [May 18, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980/3 "2006-05-18T18:21:48Z")

</div>

> Just to be sure… you did define @categories in your controller right?  
> something like…
> 
> ```
> def new
> @categories = Catgory.find(:all)
> end
> 
> ```

Yes I did. Don’t know what part of the controller conflicts with the  
model. Actually, instead of using ‘recipe’ I’m using ‘article’ so the  
model article.rb looks:

class Article \< ActiveRecord::Base

belongs\_to :category  
validates\_uniqueness\_of :title  
validates\_length\_of :title, :within =\> 1…20

## end

The article\_controller.rb is:

class ArticleController \< ApplicationController

```
     scaffold :article

     def index
         list
         render_action 'list'
     end

     def list
             @category = @params['category']
             @articles = Article.find_all
     end

     def edit
             @article = Article.find(@params["id"])
             @categories = Category.find_all
     end

     def new
             @article = Article.new
             @categories = Category.find_all
     end

     def create
             @article = Article.new(@params['article'])
             @article.created = Date.today
             if @article.save
                     redirect_to :action => 'list'
             else
                     render_action 'new'
             end
     end

     def delete
             Article.find(@params['id']).destroy
             redirect_to :action => 'list'
end

```

## end

And the view for a new article is new.rhtml:

Nuevo Articulo
# Nuevo Articulo

**Titulo**

**Descripcion**

**Categoria:**  
\<% @categories.each do |category| %\> \<%= category.name %\>

\<% end %\>

[Volver](https://www.ruby-forum.com/article/list) \</html ----------------------------------------------------------------------------------------------------

Any idea?

Cheers

Antonio

> if you didn’t then you would most definately get that error.
> 
> jon

# –

## Por favor, si me mandas correos con copia a varias personas, pon mi direcciÃ³n de correo en copia oculta (CCO), para evitar que acabe en montones de sitios, eliminando mi privacidad, favoreciendo la propagaciÃ³n de virus y la proliferaciÃ³n del SPAM. Gracias.

# If you send me e-mail which has also been sent to several other people, kindly mark my address as blind-carbon-copy (or BCC), to avoid its distribution, which affects my privacy, increases the likelihood of spreading viruses, and leads to more SPAM. Thanks.

---

<div class="post-metadata">

**Author:** ![antonio\_rodriguez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@antonio\_rodriguez](https://www.ruby-forum.com/u/antonio_rodriguez)\
**Post date:** [May 18, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980/4 "2006-05-18T18:31:13Z")

</div>

I recently had this very same problem using collection\_select.

The problem lies in that the create method does not actually run the  
new method on error. So what happens is category is null when your  
validation fails.

You have to do a find for your categories in the create method as  
well. Like so:

```
   def create
             @article = Article.new(@params['article'])
             @article.created = Date.today
             if @article.save
                     redirect_to :action => 'list'
             else
		@categories = Category.find_all
                     render_action 'new'
             end
     end

```

I posed the question that this seems to defeat the DRY methodology? I  
personally do not know Ruby well enough to identify maybe a better way.

Thanks,  
Phill

---

<div class="post-metadata">

**Author:** ![antonio\_rodriguez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@antonio\_rodriguez](https://www.ruby-forum.com/u/antonio_rodriguez)\
**Post date:** [May 18, 2006, 9:36pm UTC](https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980/5 "2006-05-18T21:36:53Z")

</div>

Does your view have a place to display errors? Such as:

\<%= error\_messages\_for ‘articles’ %\>

Also I noticed you still have

scaffold :article

in your controller. You may want to copy your controller code you  
have so far and rebuild using

> ./script/generate scaffold ModelName ControllerName

at the command line. Then merge your changes. The scaffold call in  
your controller may be interfering with your methods in your controller.

I had to add the find code to all form methods in my controller -  
edit, new, create, and update. Your update method must be coming  
from your scaffold :article line.

Thanks,  
phill

---

<div class="post-metadata">

**Author:** ![antonio\_rodriguez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@antonio\_rodriguez](https://www.ruby-forum.com/u/antonio_rodriguez)\
**Post date:** [May 18, 2006, 9:27pm UTC](https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980/6 "2006-05-18T21:27:19Z")

</div>

Phillip Novess  
escribió:\> @article = Article.new(@params[‘article’])

> ```
> @article.created = Date.today
> if @article.save
> redirect_to :action => 'list'
> else
> @categories = Category.find_all
> render_action 'new'
> end
> end 
> 
> ```

Getting better

Now I do not have any message. The ‘New article’ template does nothing  
just waits for the right insert. But doesn’t alert me about duplicate or  
empty fields.

Cheers,

Antonio

## ===== Por favor, si me mandas correos con copia a varias personas, pon mi dirección de correo en copia oculta (CCO), para evitar que acabe en montones de sitios, eliminando mi privacidad, favoreciendo la propagación de virus y la proliferación del SPAM. Gracias.

# If you send me e-mail which has also been sent to several other people, kindly mark my address as blind-carbon-copy (or BCC), to avoid its distribution, which affects my privacy, increases the likelihood of spreading viruses, and leads to more SPAM. Thanks.

---

<div class="post-metadata">

**Author:** ![antonio\_rodriguez](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/aeb1de/32.png) [@antonio\_rodriguez](https://www.ruby-forum.com/u/antonio_rodriguez)\
**Post date:** [May 19, 2006, 3:56pm UTC](https://www.ruby-forum.com/t/newbie-problem-when-using-validates/94980/7 "2006-05-19T15:56:57Z")

</div>

> Also I noticed you still have
> 
> scaffold :article
> 
> in your controller. You may want to copy your controller code you  
> have so far and rebuild using
> 
> > ./script/generate scaffold ModelName ControllerName
> 
> Thanks,  
> phill

I agree! that was the first thing i noticed about all that code. i’ve  
never had a problem with a scaffold before.

Jon
