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 2 Error - NoMethodError in Stories#show

Working through the book and running into an error on page 283. (not able to cut & paste in everything as forum is giving me an error about controlling spam by not allowing anyone with under 10 posts to post links or email)

input text from pg 283 to show name of user who submitted story.
Line 18 is the span statement with ...story.user.login

Error:

NoMethodError in Stories#show

Showing where line #18 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.login

The error means that your application is returning a nil for story.user. Have a look at the database and see what user id is given in the user_id field in the story you are trying to view. If there is no entry, then that is your problem, and you need to check the code that is adding or updating the user_id.

If the story has an id number in user_id, then the next thing to check is that there is an User with that id. You would get this error if the user has been deleted that was associated with this story. If this is the error you may want to look at adding code that handles the situation where users are deleted and their stories are not.

If there is a user_id and a matching user with that id, then it is probably the table association that is at fault. That is, a problem associated with the belongs_to :user statement. For example, you will get an error if your user table is called "user" rather than "users".

It is always worth trying these sort of thing out at the console. You can access this via:

Code:

ruby script/console

From the root of your application. Once in you can check that a story (e.g. 1) has a user (e.g. 3) like this:

I forget where it is in the book, but I remember something about entering Stories before the login is added, and then needing to "modify the old"/"delete and add new" Stories after the login has been added. This is something a few have "skipped over" and had trouble with because the book is a bit unclear about it.

Can you help a newbie with how to manually insert these values into the DB?

How you do it via the database depends on the database engine you are using.
Personally, I like using MySQL as you can install the Administrator GUI tools and that makes it easy to monitor and update the datebase directly. You can then run SQL commands directly via the MySQL Query Browser. For example UPDATE.

However, if you are only just starting to program, you may want to keep to Ruby and Rails at the moment, and move on to SQL hacking when you have those under your belt. In that case, I'd suggest the following:

Go to the root of your application at the console (cmd in Windows). Then go into Rails console mode from that point. On Windows that will probably look something like this:

Code:

c:\rails_root\application\>ruby script/console

Then do the following:

Code:

# find the first user
user = User.find(:first)
# then get all the stories
stories = Story.find(:all)
# then assign the first user to all the stories
for story in stories
story.user = user
story.save
end
exit