NoMethodError in / Nil Object error

This is one of those bugs that drives a person crazy, but it's probably something simple that I'm missing (as newb to rails dev).

Here's my problem. I have the following controller.

def new    @document = Document.new    @document.title = 'my test title' end

Then in the view (new.rhtml) I have

<%= @document.title >

This gives me the NoMethod Error in Document nil Object when I didn't expect it error.

It doesn't get much simpler than this, right? Why would this fail. It's making me crazy.

Does your class code for Document contain a @title variable? And if it does, does it contain either an attr_reader or attr_accessor declaration?

-- gw (www.railsdev.ws)

No, Title isn't a method in the class. It's a column in the DB. I'm trying to use active record here to access title in the DB.

To be clear too, the error message specifically is "The error occurred while evaluating nil.title" The funny part is that I have the debug(@document) in the view and it's empty. So it seems like the instance variable is never made.

I'm being picky here but should the code read

<%= @document.title %>

As in the second % is missing - not sure what this would do though. It might be something to do with plural and singulars in the view directory names confusing the routing, but it wouldn't be able to find the template at all.

Did you generate with scaffold or rspec_scaffold? If you didn't I'd suggest generating another entity, say doc, and seeing where the code differs.

rake script/generate scaffold_resource Doc title:string

(I think scaffold's been taken out of 2.0 but it's worth looking at what gets generated if you're pre 2.0)

HTH

Thanks for the tip about using logger to debug. So I updated my method in the controller and added a logger call to insert foo and I didn't see it, which indicated that the action isn't being called. But from the log files it looks like the action is being called....

DocumentController::DocumentController: missing default helper path document_controller/document_helper

Processing DocumentController#new (for 127.0.0.1 at 2007-11-13 20:11:53) [GET]   Session ID: 025b513c214398f0c25466623b80fefa   Parameters: {"action"=>"new", "controller"=>"document"} Rendering within layouts/application Rendering document/new Completed in 0.00449 (222 reqs/sec) | Rendering: 0.00280 (62%) | DB: 0.00000 (0%) | 200 OK [http://localhost/document/new\]

I'm truly puzzled by this. At one point I had a custom route for my document calss, but I took it out when I though it was a problem Can routes be cached?

I just noticed the note about missing default helper path document. How do I fix that?

Thanks for point me in the right direction everyone. I figured out what happened. When I was cutting and pasting some code I accidentally copied the controller class declaration. So I had a nest controller declaration.. (e.g.

Class DocumentController < ApplicationController      Class DocumentController < ApplicationController

     A lot of code I wrote here...

    end end

I just didn't see the forest for the trees. Thanks again, everyone.