Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Ruby Rails Routes and Resources A Route to an Index Action Adding Records via Rails Console

David Thrower
David Thrower
6,693 Points

Console gives me NameError: undefined local variable or method `page' for main:Object on > page.title = "lorem ipsum"

OK so i follow all the steps to here verbatim with the same results up to here. The controller and model are there, the migration has been run, and etc. I get to the console to add dummy records:

$ rails c
> Page.all
"Page Load (1.4ms)  SELECT "pages".* FROM "pages"
 => #<ActiveRecord::Relation []>" 
> Page.new
-> <Page id: nil, title: nil, body: nil, slug: nil, created_at: nil, updated_at: nil>
page.title = "lorem ipsum"

-> NameError: undefined local variable or method `page' for main:Object

(then lots of stack tracings)

Yes, I did check that the capitalization / pluralization is correct. Anyone know what this is wrong?

1 Answer

Hi David,

Did you get this fixed?

From what I can see, you have not assigned your new Page instance into the variable page so when you try to set the title attribute, the console doesn't know what page is.

To fix this, assign the result of calling new into page:

page = Page.new

I hope that helps.