problem with validation

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?

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

Mazraelle Mazraelle 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

Mazraelle Mazraelle 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

<td>

          <%= text_field :jour, params[:jour]%>         </td>         <td>             <%= select('fraisreel','lot_id',@lots,{:selected =>@selected_lot},{}) %>         </td>         <td class="tt">             <%= text_field 'fraisreel', 'hotel', "size" =>6 ,"class"=>"sChamps" %>€         </td>         <td class="tt">             <%= text_field 'fraisreel', 'restaurant', "size" =>6,"class"=>"sChamps" %>€         </td> <%= 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: </td> 45: <td> 46: <%= select('fraisreel','lot_id',@lots,{:selected =>@selected_lot},{}) %> 47: </td> 48: <td class="tt"> 49: <%= text_field 'fraisreel', 'hotel', "size" =>6 ,"class"=>"sChamps" %>€

Mazraelle Mazraelle 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.