NoMethodError in Lists#index

Does anyone know how to fix the error at the end of the path /home/rails/example/app/views/lists/index.html.erb? The error is undefined methodeach’ for nil:NilClass`.

I’m guessing I need to define the method `each’, but I don’t know where to go in my rails app to do this or an example of a definition to use.

HI @codyrutscher,
you don’t need to define the each method.

That’s part of Ruby.

The problem is with this nil:NilClass part.

Nil is given to you when you’ve an object that is “empty” or “undefined”.

So what’s happening in your list view is that you’re trying to call each on an undefined object.

That’s exactly why you’re getting this error.

Here’s how to fix it:

<% @lists&.each do |list| %>
  # ...
<% end %>

I’m assuming that the name of your variable is @lists, if that’s not the case then you want to change it to whatever you’re currently using.

To be clear, what I want you to do is to open your index.html.erb file, then locate the section of code where you use the each method, then make sure that you have this ampersand symbol before the dot.

This is called the safe navigator operator.

Here are some topics you may want to read about before you continue with your Rails journey:

  • Ruby each method
  • Ruby objects & classes
  • Ruby nil

Please follow the instructions, don’t overthink this.

Hope that helps :slight_smile:

That may have helped. However, when you go to the domain gitship.com, an error still comes up. Not sure what to do. I may start from scratch.

You need to learn how to deal with errors, not avoid them.

To do that you need 3 things:

  1. Patience
  2. Reading & learning. Specifically about Ruby (not just Rails) & the topics I’m recommending to you
  3. Research. Use google to search for the error message, read different solutions, try them, try to understand what’s going on.

Also read this:

https://guides.rubyonrails.org/routing.html

I’m assuming that you’re learning Ruby to become a professional developer. If that’s not your goal then I need to know that so I can give you different advice.

1 Like

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs