Luiz E.
Luiz E.

Reputation: 7279

Getting undefined method `each` on my controller

I have a method like this :

class ProfilesController < ApplicationController
  before_filter :authenticate_user!

  current_user

  def index
    @users = User.all
  end
  ...
end

and my route is

match 'profile',  :controller => 'profiles', :action => 'index'

but when I access http://127.0.0.1:8080/profile I get:

NoMethodError in Profiles#index undefined method each for nil:NilClass

Upvotes: 0

Views: 2090

Answers (2)

Luiz E.
Luiz E.

Reputation: 7279

Worked. I just removed the line current_userfrom my ProfilesController @Jesper do this have something with the filter you said?

Upvotes: 0

Andrew
Andrew

Reputation: 43153

This means User.all is returning nil. You need to check if there are any users before calling each on them. If you changed your view to look like this it would not raise the error.

<% if @users %>          
  <% @users.each do |user| %>
    ...
  <% end %>
<% end %>

Upvotes: 2

Related Questions