Ruby on Rails Error: The error occurred while evaluating nil.each?

You should make a partial and put that post logic inside of it Then you can put it on any view as long as you initialize the collection inside of each used controller action So in your home controller you'd still need: posts = Post. Find(:all) As far as your first question, try finding them via posts = Post. Find(:all).

You should make a partial and put that post logic inside of it. Then you can put it on any view as long as you initialize the collection inside of each used controller action. So in your home controller you'd still need: @posts = Post.

Find(:all) As far as your first question, try finding them via @posts = Post. Find(:all).

Thanks Chuck that worked. – Vlad. P Dec 2 '10 at 22:06 No problem!

Happy trails. – Chuck Dec 3 '10 at 3:00.

Add a route to routes. Rb (for Rails 2. X only): map.

Root :controller => "posts", :action => "index" For Rails 3. X have a look here: guides.rubyonrails.org/routing.html#usin... The error occurred while evaluating nil. Each means that @posts is not an Array.

Have you created a posts table?

Yes, I can see them on the /posts but I was thinking of a global variable type for displaying in home/index.html.erb. Is there such a thing? – Vlad.

P Dec 2 '10 at 16:34 How can you see the posts and get the nil. Each error at the same time? Something doesn't make sense.

– Mark Thomas Dec 2 '10 at 20:06 I get the nil. Each error only in index.html. Erb file, that is actually the root but in the /posts view I can see them.

But I wish to list them into the home (root). – Vlad. P Dec 2 '10 at 22:00.

The error is (as you probably suspected) happening in the following line: Somehow @posts ends up being nil, even though we want it to be a list. That's a little weird, because the code above looks like it should work. I'll try to help you track down the problem.Post.

All is equivalent to Post. Find(:all), and according to the documentation for find, it could return nil, but I don't know when this would ever happen in practice. Normally it should return an empty list () if there are no posts in the database, or raise an exception if the database table doesn't exist at all.So just in case Post.

All somehow returns nil, try @posts = instead of @posts = Post. All in the controller. The .

Each should work then. If (as I suspect), it doesn't, my guess is that somehow a different controller from the one you posted above gets executed. Then, because @posts has never been set, trying to access it will simply give you nil, which explains the error.

So check your paths and class names, and make sure that the code you think gets run actually gets run. Hope that helps you figure out what's going wrong -- and let us know what solved the problem for you!

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions