6

So I'm following the official ROR tutorial at http://guides.rubyonrails.org/getting_started.html and I am stuck at section 5.8 where it teaches me how to list out all the articles

The following are my controller and index.html.erb

controller

class ArticlesController < ApplicationController
  def new
  end


  def create
    @article = Article.new(article_params)

    @article.save
    redirect_to @article
  end

  def show
    @article = Article.find(params[:id])
  end

  def index
    @article = Article.all
  end


  private
  def article_params
    params.require(:article).permit(:title, :text)
  end


end

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>

I am getting the NoMethodError in Articles#index with the error message

undefined method `each' for nil:NilClass"

What's wrong? I literally copied and pasted the code from the website to see what I'm doing wrong, but still cannot fix it.

3 Answers 3

18

Use @articles and not @article

def index
  @articles = Article.all ## @articles and NOT @article
end

@articles (plural) is semantically correct as you would be displaying a collection of articles in your view and not a single article.

You are getting error

undefined method `each' for nil:NilClass

because in the index action, you have instantiated the instance variable @article(NOTICE singular) and are using @articles(NOTICE plural) in your index view i.e., index.html.erb. So, in the view @articles(plural) would be nil as it was never set. Hence, the error.

2
  • 1
    oh wow I can't believe I missed that after googling answers for the past hour.. thanks! Commented May 13, 2014 at 18:42
  • 3
    thanks very much for the clear explanation, it helped to lead me to fix the problem I had.
    – yesiamtom
    Commented Oct 18, 2014 at 12:37
2

Write as

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

You have defined @article and not @articles. But good name is @articles, as it means collection of all article, thus use it in both places. If you didn't define and attempt to use the any instance variable, it will return nil. Now Nilclass#each not exist, thus you got a valid error.

2

The index action

def index
  @articles = Article.all      
end
OR
def index
  @article = Article.all      
end

Both @article or @articles will work in views. But it depends upon you which one index action is taken.

If @article is taken then in view it should be

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

If @articles is taken then in view it should be

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

Note:- It is good practice to take pluralize of instancle variable when it is a collection method

Hope this makes you clear on @articles and @article

2
  • 3
    Some explanation of your code might be in order. A block of code by itself doesn't usually constitute a complete answer.
    – user684934
    Commented May 13, 2014 at 18:46
  • yes A block of code by itself doesn't usually constitute a complete answer but my answer was right. So if it not voted up then it should not be voted down. Commented May 13, 2014 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.