# Problem with validation

**URL:** <https://www.ruby-forum.com/t/problem-with-validation/138577>\
**Category:** Rails\
**Created:** [May 25, 2008, 12:03am UTC](https://www.ruby-forum.com/t/problem-with-validation/138577 "2008-05-25T00:03:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![mazraelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mazraelle](https://www.ruby-forum.com/u/mazraelle)\
**Post date:** [May 25, 2008, 12:03am UTC](https://www.ruby-forum.com/t/problem-with-validation/138577/1 "2008-05-25T00:03:55Z")

</div>

hi

i define in my model for example this validation: validates\_presence\_of  
:commentaires  
but when in my form my field is empty i have this error

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

Can you help me?

---

<div class="post-metadata">

**Author:** ![mazraelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mazraelle](https://www.ruby-forum.com/u/mazraelle)\
**Post date:** [May 25, 2008, 2:42am UTC](https://www.ruby-forum.com/t/problem-with-validation/138577/2 "2008-05-25T02:42:54Z")

</div>

It would help I think if you could past the last five to 10 lines of the  
stack trace.

---

<div class="post-metadata">

**Author:** ![mazraelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mazraelle](https://www.ruby-forum.com/u/mazraelle)\
**Post date:** [May 25, 2008, 3:32am UTC](https://www.ruby-forum.com/t/problem-with-validation/138577/3 "2008-05-25T03:32:56Z")

</div>

Mazraelle M. wrote:

> hi
> 
> i define in my model for example this validation: validates\_presence\_of  
> :commentaires  
> but when in my form my field is empty i have this error
> 
> You have a nil object when you didn’t expect it!  
> You might have expected an instance of Array.  
> The error occurred while evaluating nil.inject
> 
> Can you help me?

i had the same issue. mine was because i needed to set my arrays again  
from other models before the redirection back to the new page again.

here’s what i had to do:  
def new  
@item = Item.new  
@item\_types = ItemType.find(:all)  
@attributes = Attribute.find(:all)  
@wears = Wear.find(:all)

```
respond_to do |format|
  format.html # new.html.erb
  format.xml { render :xml => @item }
end

```

end

then in my create function i had to set the variables again:

def create  
@item = Item.new(params[:item])

```
respond_to do |format|
  if @item.save
    flash[:notice] = 'Item was successfully created.'
    format.html { redirect_to(@item) }
    format.xml { render :xml => @item, :status => :created, 

```

:location =\> @item }  
else  
@item\_types = ItemType.find(:all)  
@attributes = Attribute.find(:all)  
@wears = Wear.find(:all)

```
    format.html { render :action => "new" }
    format.xml { render :xml => @item.errors, :status => 

```

:unprocessable\_entity }  
end  
end  
end

hope that helps

---

<div class="post-metadata">

**Author:** ![mazraelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mazraelle](https://www.ruby-forum.com/u/mazraelle)\
**Post date:** [May 25, 2008, 10:38am UTC](https://www.ruby-forum.com/t/problem-with-validation/138577/4 "2008-05-25T10:38:18Z")

</div>

def new

```
@fraisreel = Fraisreel.new
@salarie_id =session[:user_id]
liste_deroulante

@mois = session[:mois]
@annee = session[:annee]
@date = @annee + "-" + @mois

```

end

def create  
@jour\_form = params[:jour]  
params[:fraisreel][:date\_jour]+="-" + @jour\_form.to\_s  
@fraisreel\_form = Fraisreel.new(params[:fraisreel])

```
if @fraisreel_form.save
  flash[:notice] = 'La nouvelle ligne de Frais Reel a Ã©tÃ© ajouter 

```

avec succes.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def liste\_deroulante  
@resultat = Fraisreel.find\_liste\_affaire\_salarie(session[:user\_id])  
for resultat in @resultat  
@resultat2 = Fraisreel.find\_liste\_lot(resultat.projetdept)  
@lots1 = @resultat2.map{|of| [resultat.code\_projet + " - " +  
of.code\_lot, of.lotid]}  
end  
@resultat = Fraisreel.find\_liste\_affaire\_tous\_le\_monde  
@lots2 = @resultat.map{|of| [of.code\_projet + " - " + of.code\_lot,  
of.lotid]}  
@lots = @lots2 + @lots1  
end

in my view

```
      <%= text_field :jour, params[:jour]%>
    </td>
    <td>
        <%= select('fraisreel','lot_id',@lots,{:selected 

```

=\>@selected\_lot},{}) %\>

  
  
\<%= text\_field ‘fraisreel’, ‘hotel’, “size” =\>6  
,“class”=\>“sChamps” %\>â‚¬  
  
  
\<%= text\_field ‘fraisreel’, ‘restaurant’, “size”  
=\>6,“class”=\>“sChamps” %\>â‚¬  
  
\<%= hidden\_field(‘fraisreel’,‘date\_jour’,:value =\>@date)%\>

---

<div class="post-metadata">

**Author:** ![mazraelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mazraelle](https://www.ruby-forum.com/u/mazraelle)\
**Post date:** [May 25, 2008, 10:40am UTC](https://www.ruby-forum.com/t/problem-with-validation/138577/5 "2008-05-25T10:40:53Z")

</div>

Mazraelle M. wrote:

> def new
> 
> ```
> @fraisreel = Fraisreel.new
> @salarie_id =session[:user_id]
> liste_deroulante
> 
> @mois = session[:mois]
> @annee = session[:annee]
> @date = @annee + "-" + @mois
> 
> ```
> 
> end
> 
> def create  
> @jour\_form = params[:jour]  
> params[:fraisreel][:date\_jour]+="-" + @jour\_form.to\_s  
> @fraisreel\_form = Fraisreel.new(params[:fraisreel])
> 
> ```
> if @fraisreel_form.save
> flash[:notice] = 'La nouvelle ligne de Frais Reel a Ã©tÃ© ajouter 
> 
> ```
> 
> avec succes.’  
> redirect\_to :action =\> ‘list’  
> else  
> render :action =\> ‘new’  
> end  
> end
> 
> def liste\_deroulante  
> @resultat = Fraisreel.find\_liste\_affaire\_salarie(session[:user\_id])  
> for resultat in @resultat  
> @resultat2 = Fraisreel.find\_liste\_lot(resultat.projetdept)  
> @lots1 = @resultat2.map{|of| [resultat.code\_projet + " - " +  
> of.code\_lot, of.lotid]}  
> end  
> @resultat = Fraisreel.find\_liste\_affaire\_tous\_le\_monde  
> @lots2 = @resultat.map{|of| [of.code\_projet + " - " + of.code\_lot,  
> of.lotid]}  
> @lots = @lots2 + @lots1  
> end
> 
> in my view
> 
> ```
> <%= text_field :jour, params[:jour]%>
> </td>
> <td>
> <%= select('fraisreel','lot_id',@lots,{:selected 
> 
> ```
> 
> =\>@selected\_lot},{}) %\>
> 
>   
>   
> \<%= text\_field ‘fraisreel’, ‘hotel’, “size” =\>6  
> ,“class”=\>“sChamps” %\>â‚¬  
>   
>   
> \<%= text\_field ‘fraisreel’, ‘restaurant’, “size”  
> =\>6,“class”=\>“sChamps” %\>â‚¬  
>   
> \<%= hidden\_field(‘fraisreel’,‘date\_jour’,:value =\>@date)%\>

here there is the next line in the error page  
Extracted source (around line #46):

43: \<%= text\_field :jour, params[:jour]%\>  
44:   
45:

  
46: \<%= select(‘fraisreel’,‘lot\_id’,@lots,{:selected  
=\>@selected\_lot},{}) %\>  
47:   
48:   
49: \<%= text\_field ‘fraisreel’, ‘hotel’, “size” =\>6  
,“class”=\>“sChamps” %\>â‚¬

---

<div class="post-metadata">

**Author:** ![mazraelle](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/58f4c7/32.png) [@mazraelle](https://www.ruby-forum.com/u/mazraelle)\
**Post date:** [May 25, 2008, 3:07pm UTC](https://www.ruby-forum.com/t/problem-with-validation/138577/6 "2008-05-25T15:07:34Z")

</div>

Mazraelle M. wrote:

> Extracted source (around line #46):
> 
> 46: \<%= select(‘fraisreel’,‘lot\_id’,@lots,{:selected

This line (erb line of code) is diving off into ruby code. The  
log/development.log file will have a full stack trace back. You will  
see this line a few lines down from the top. The next line up will  
probably be a call to select. etc. You need to follow the stack and  
see which variable it is trying to use when it gets the error.

My _guess_ is that @lots is empty. You can do a couple of things.

One thing would be to add a debug line in your controller method just  
before calling the render (or just before it returns). Print out @lots,  
etc.

Another possibility (I’ve never tried this) \<% debugger %\> just before  
this line. Then invoke your server with ./script/server --debugger

When you hit the line before, it will plop into the debugger. You can  
then poke around and see what is happening.
