SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [rails] nil object error

    [edited to add - if i input valid information, the code below works as intended - the new mileage object is added to the db. the error comes in when i don't add valid data]

    i have a problem with an unexpected nil object. the error is as follows:

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

    I use pgsql as my database. i have a few columns set up as not null:

    Code:
    class CreateMileages < ActiveRecord::Migration
      def self.up
        create_table :mileages do |t|
          t.column :title,           :string,   :null => false
          t.column :start,           :integer,  :null => false
          t.column :stop,            :integer,  :null => false
          t.column :details,         :text,     :null => false
          t.column :vehicle_id,      :integer,  :null => false
          t.column :mileage_date,    :date,     :null => false
          t.column :entry_date,      :date,     :null => false
          t.column :submitted,       :boolean
          t.column :submitted_date,  :date
          t.column :reimbursed,      :boolean
          t.column :reimbursed_date, :date
        end
      end
    
      def self.down
        drop_table :mileages
      end
    end
    when i try and enter a blank form from new.rhtml (sent to the "create" action), i get the expected pg error for trying to submit null data when it should be not null.

    so far, so good - or at least as expected.

    however, when i validate_presence_of :title i receive the nil object error mentioned at the top of this post.

    here is the code from my other files:

    [code]
    class MileageController < ApplicationController

    scaffold :mileage

    #stuff here

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

    #more stuff here

    [\code]

    nileage.rb:

    Code:
    class Mileage < ActiveRecord::Base
      
      belongs_to :vehicle
      
      validates_presence_of :title
      #validates_numericality_of :start, :stop
    
    end
    can anyone help with this issue?

    i'm wondering if it has to do with mileage (belongs_to :vehicle) and vehicle (has_many :mileages) being linked together in this code.

    tia...
    Last edited by skeeterbug; Jan 5, 2007 at 22:27.

  2. #2
    SitePoint Member
    Join Date
    Nov 2006
    Location
    Austin, TX
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What's your view code look like? I suspect it's a problem there.

    Also a few tips. You should use "params" instead of "@params", and render :action => 'new' instead of render_action. I think both of those have been deprecated.
    wtf242
    ProgrammingBooks.org - Programming Books Ranked by Programmers
    Turn of the Crank - My blog
    Robot Walrus - My Art Prints/Posters Blog

  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2005
    Posts
    485
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wtf242 View Post
    What's your view code look like? I suspect it's a problem there.

    Also a few tips. You should use "params" instead of "@params", and render :action => 'new' instead of render_action. I think both of those have been deprecated.
    thanks for the guidance!

    i think i have it. my create method renders new.rhtml which relies on the @vehicles array being available (provides a drop down of available vehicles). i changed "render" to "redirect_to" in order to allow the action to create the array and all is good... almost (see below).

    new.rhtml offending code (no array was available):

    Code:
            <b>Vehicle:</b><br/>
            <select name="mileage[vehicle_id]">
              <% @vehicles.each do |vehicle| %>
                <option value="<%= vehicle.id %>"> 
                  <%= vehicle.make + ' ' + vehicle.model + ', ' + vehicle.license %>
                </option>
              <% end %>
            </select>
    @vehicles was nil and kicking the error.

    the reason i saw this error is b/c rails rejected the input based upon validation in the model. however, i expected an error message to display. now that i've slept on my code, i guess i'm not following standard operating procedure in order to get the automatic error display to display.

    does the error message get written to the flash? do i need to display the flash to see the validates_presence_of error messages?

    does my redirect_to kill the contents of the flash?

    basically, i want all the goodness of those automatic validation errors so i don't have to code them myself.

    i'm off to play around and see what i can do. i'll report back with my findings.

    if someone can chime in with some information, i'd appreciate that, too.

    also, thanks for pointing out the deprecated code. following old tutorials as a newbie not familiar with the latest and greatest is challenging.

  4. #4
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    11
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Similar issue

    I'm having the exact same issue but I can't seem to sort it out. I'm putting together a blog/news website of sorts.

    I thought everything was running just peachy until I tried to create a story with a headline that was already used. I have verification for uniqueness in the model.

    Unique stories work but duplicates result in an error of "You have a nil object when you didn't expect it!"

    Our code looks very similar and I don't see what's wrong with it, unless I'm doing something wrong in the controller.

    Extracted source (around line #9):

    Code:
    6: 
    7: <p><b>Author</b> 
    8:   <select name="story[author_id]">
    9:    <&#37; @authors.each do |author| %>
    10:        <option value="<%= author.id %>" 
    11:          <%= ' selected' if author.id == @story.author_id %>>
    12:          <%= author.name %>, <%= author.position %>
    Any pointers would help a lot. Thanks.

    After some tinkering and replicating what Skeeterbug did (Changing render to redirect_to) I have it working without an error. However it's not really a fix since it just ignores the error.
    Last edited by Cloretz; Jan 14, 2007 at 19:51. Reason: Update

  5. #5
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just set @vehicles in the new and in the create action. Could you also post your controller code. Maybe it isn't necessary to redirect?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •