(undefined method `each' for nil:NilClass) while doing Getting started tutorial

691 views
Skip to first unread message

Med Potter

unread,
Mar 26, 2015, 2:52:56 AM3/26/15
to rubyonra...@googlegroups.com
I followed every line of the tutorial

but I get this error

Showing /home/ubuntu/blog/app/views/articles/index.html.erb where line #9 raised:
undefined method `each' for nil:NilClass

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>


if you think that might not have returned the articles in the controller, thats not true :
here is code for my controller :
 
 $ cat app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def index
    @articles = Article.all
end


and here is code for my view
$ cat  app/views/articles/index.html.erb
<h1>Listing articles</h1>

<table>
  <tr>
    <th>Title</th>
    <th>Text</th>
  </tr>

  <% @articles.each do |article| %>
    <tr>
      <td><%= article.title %></td>
      <td><%= article.text %></td>
    </tr>
  <% end %>
</table>
 
The code works for showing one article but not for showing all articles

Vivek Sampara

unread,
Mar 26, 2015, 3:02:36 AM3/26/15
to rubyonra...@googlegroups.com
2 Possibilities. 
1. Did you run your migrations
2. Did you restart your server after that ?

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/c0e36d2f-8b86-4714-b2c1-d90beb9428ba%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Colin Law

unread,
Mar 26, 2015, 4:50:09 AM3/26/15
to rubyonra...@googlegroups.com
On 25 March 2015 at 22:12, Med Potter <medy...@gmail.com> wrote:
> I followed every line of the tutorial
>
> but I get this error
>
> Showing /home/ubuntu/blog/app/views/articles/index.html.erb where line #9
> raised:
> undefined method `each' for nil:NilClass
>
> <% @articles.each do |article| %>
> <tr>
> <td><%= article.title %></td>
> <td><%= article.text %></td>
>
>
>
> if you think that might not have returned the articles in the controller,
> thats not true :
> here is code for my controller :
>
>
> $ cat app/controllers/articles_controller.rb
> class ArticlesController < ApplicationController
> def index
> @articles = Article.all

To check that @articles is ok at this point then insert here
puts @articles.inspect
which will print the contents of @articles in the server terminal window.

Colin
Reply all
Reply to author
Forward
0 new messages