SitePoint Sponsor

User Tag List

Results 1 to 22 of 22

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Simply Rails problem on page 207

    Sorry, I posted this on Questions, then realized I should have posted it here. I started this forum today.

    I am new to Rails.
    When I browse to localhost:3000/stories, —it works.
    When I browse to localhost:3000/stories/2
    I get a NoMethodError showing stories/show.html.erb that says "You have a nil object when you didn't expect it! The error occured while evaluating nil.name"

    The Extracted Source is
    1: <h2><%= @story.name %></h2>
    2: <p><%= link_to @story.link, @story.link %></p>

    (which is from show.html.erb)

    stories_controller.rb shows
    class StoriesController < ApplicationController

    def index
    @story = Story.find(:first, :order => 'RANDOM()')
    end

    def new
    @story = Story.new
    end

    def create
    @story = Story.new(params[:story])
    if @story.save
    flash[:notice] = "Story submission succeeded"
    redirect_to stories_path
    else
    render :action => 'new'
    end

    def show
    @story = Story.find(params[:id])
    end
    end
    end

    Can anyone help?

    Thanks

  2. #2
    SitePoint Addict ruby-lang's Avatar
    Join Date
    Aug 2007
    Posts
    389
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello, and welcome to the forum.

    You probably figured out part of what's happening, but let me recap what Rails is doing step by step. When you go to localhost:3000/stories/2, the show method is called. To populate the @story instance variable, it calls Story.find, which returns a Story object if it finds one row in the database with the id you are passing, in this case 2. Now, there's no row with an id = 2 in your stories table, so find returns nil to indicate it didn't find anything.

    After show returns, Rails calls the corresponding template, stories/show.html.erb. But @story isn't a Story object, it is nil, and nil doesn't have properties like name and link. That's why you see that error. One way you can prevent that from happening is adding a check to the show template like this:
    Code:
    <% if @story %>
      <h2><%= @story.name %></h2>
      <p><%= link_to @story.link, @story.link %></p>
    <% else %>
     <p>No story found.</p>
    <% end %>

  3. #3
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I look in the SQLite Database Browser, I find that I have in row 1:
    id: 2
    name: Sitepoint Forums
    link--etc.

    I also have put 4 more id's in the stories table.

    So, why does localhost:3000/stories/2 return an error?

    Please be patient, I am not a programmer.

  4. #4
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you post the whole of show.html.erb.

    When posting to this forum, if you surround the code you post with a [code] block (or highlight it all and use # on the toolbar), the code will be displayed correctly formatted here.
    Code:
    #Like this
    def some_method
      a = b
    end

  5. #5
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you. Here is the code for show.html.erb:
    Code:
    <h2><%= @story.name %></h2>
    <p><%= link_to @story.link, @story.link %></p>

  6. #6
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't think that should generate an error. The coding is right, so the problem must be elsewhere.

    Are you looking at the right SQLite database. Don't forget that if you are looking at the development version of your app, the database will be called something like:

    app_name_development

    and when you run in production mode the system uses a different database called something like

    app_name_production

    Unless you have rewritten the database.yml to have both production and development environments pointing at the same database.

  7. #7
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webmathetic View Post
    When I look in the SQLite Database Browser, I find that I have in row 1:
    id: 2
    name: Sitepoint Forums
    link--etc.

    I also have put 4 more id's in the stories table.

    So, why does localhost:3000/stories/2 return an error?

    Please be patient, I am not a programmer.
    Assuming that, as you are looking at localhost:3000, this is a development view (you haven't specifically told the mongrel or webrick to use product mode):

    When you use SQLite Database Browser, are you sure you are looking at the development.sqlite3 database?

  8. #8
    SitePoint Member
    Join Date
    Aug 2008
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the path to my database, as shown in SQLite Database Browser:
    Code:
    C:\InstantRails\rails_apps\shovell\db\development.sqlite3
    Of course, this is Windows. The book suggested that I put the InstantRails folder in the C drive.

  9. #9
    SitePoint Evangelist
    Join Date
    Feb 2006
    Location
    Worcs. UK
    Posts
    404
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That all looks fine.

    What is the error you get?


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
  •