SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I "have a nil object, when I didn't expect it"

    I'm on page 340 in the Simply Rails book, but when I was trying to test the Shovell application, the application catches an error from a previous part of the book.
    The error is:
    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.size

    Here's the problematic code:
    1: <h2>
    2: <%= "Showing #{ pluralize(@stories.size, 'story') }" %>
    3: </h2>
    4: <% render artial => @stories %>

    There is something wrong with line 2. I am too unfamiliar with Ruby code to figure it out.

  2. #2
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Kelp, the problem may actually be in your controller. The instance variable @stories seems to be nil, or none existant when the view goes to reference it. Can you show me the code in your stories controller?

  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    71
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sure, I will post it here:
    Code:
    class StoriesController < ApplicationController
      before_filter :login_required, :only => [ :new, :create]
      def index
        @story = Story.find :all,
          :order => 'id DESC',
          :conditions => 'votes_count >= 5'
      end
      def new
        @story = Story.new
      end
      def create
        @story = @current_user.stories.build params[:story]
        @story = Story.new(params[:story])
        if @story.save
          flash[:notice] = 'Story submission succeeded'
          redirect_to stories_path
        else
          render :action => 'new'
        end
      end
      def show
        @story = Story.find(params[:id])
      end
    
    end

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2010
    Location
    Cleveland, OH
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only thing that I can see would create the nil object is the condition on the find method. You are limiting the result set to stories with at least five votes. Try either lowering the condition to just one vote or going to the console to make sure you have at least one story that meets the five vote minimum.

  5. #5
    SitePoint Member
    Join Date
    Sep 2009
    Location
    Czech Republic | Portugal
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in the controller you an instance variable @story but you are referencing it as @stories, have you tried to reference it as @story in he view as the controller is not passing any @stories to the view therefor "nil.size"

    Let me know if you solved it


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
  •