SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    @story is nil but Story.find(:first) shows a result

    I am working on Simply Rails 2. This problem occurs around page 346

    I have several stories Story.find(first) returns
    Code:
    #<Story id: 1, name: "Ogg invades", link: "http://climatecalm.org", created_at: "2009-07-18 10:44:51", updated_at: "2009-07-28 17:09:33", user_id: 1, votes_count: 3>
    This is an extract from show.html.erb
    Code:
    <h2>
      <span id="vote_score">
        Score: <%= @story.votes_count %>
      </span>
      <%= @story.name %>
    </h2>
    It gives the error:
    You have a nil object when you didn't expect it!
    The error occurred while evaluating nil.votes_count

    This page was working. Does anybody know what's happening?

  2. #2
    SitePoint Enthusiast
    Join Date
    Mar 2001
    Location
    Hailsham, UK
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does your show action in StoriesController find a story and assign it to @story?

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2007
    Posts
    76
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The show action was placed below the fetch_stories action that is protected
    Thanks for the hint

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably easiest if you could post your controller code.

  5. #5
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,189
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)
    Page 346 is functional tests. My show.html.erb file looks like
    Code Ruby:
    <h2>
      <span id="vote_score">
        Score: <%= @story.votes.size %>
      </span>
      <%= @story.name %>
    </h2>
    ......
    Maybe you missed an edit?


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
  •