The SitePoint Forums have moved.

You can now find them here.
This forum is now closed to new posts, but you can browse existing content.
You can find out more information about the move and how to open a new account (if necessary) here.
If you get stuck you can get support by emailing forums@sitepoint.com

If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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"

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:

I still can't see anything wrong. I think it must be your InstantRails installation. You could reinstall InstantRails, but I think a proper Rails installation on XP is so simple than you are better doing that than using InstantRails.

However, before trying that, there is one thing I would suggest doing:

If it doesn't produce an error, have a look at the development.log entry. As you did a Story.find at the controller, the resulting SQL should appear in the log. If you can't work out which part of the log relates to this page request do this:

Shut down the web server (stop mongrel)

Rename development.log to old_development.log

Start the web server.

Enter the URL for your show page page (or refresh the page if it is already open) in your browser.

What you get in the development.log now pure contains what happened since you renamed the log file.

That tells me that the problem is not between your application and the database, but rather between the controller and the view template. That is, data created at the controller is not being passed to the view template.

There is something fairly fundamentally wrong with your Rails installation. I think the simplest and easiest solution would be to reinstall.

Originally Posted by webmathetic

If I have to reinstall InstantRails, do I have to start back over in the book and work back up to this point?

No ... but ...

You can copy your whole application to another location to back it up. One of the great things about Rails is that all the application files are simple text files so you can move them very easily.

So copy your application folder (from an earlier post this looks like shovell at "C:\InstantRails\rails_apps\shovell" on your system) to another location.
Then uninstall and reinstall InstantRails.
Then copy the shovell folder back in to the new InstantRails\rails_apps folder.

And the but! There is a chance the problem is in the application code somewhere. If you follow the instructions and get the same problem, it would probably be simpler to start again.

I've been working trough a few tutorials and have noticed that controller classes are singular (a Ruby "convention"). i.e.
using
> ruby script/generate controller Story
the created file story_controller.rb does NOT look like
class StoriesController < ApplicationController
but rather
class StoryController < ApplicationController

I can't use InstantRails, but you need to be online when you
gem update/install.
This is unfortunate as it seems relatively little time is spent with actual transfer. Most of the time is Gems extracting files etc., but as you don't know when it will make another request, if you go offline the whole process could get aborted.