Reputation: 53
I'm following mhartl's tutorial at ruby.railstutorial.org and I'm having an issue with the index function of the users_controller. In the console, I can enter this code:
@users = User.all
and it properly accesses the SQL database and displays the list of all users. However, when I try to load the page I get various errors depending on how I use the list in the index file, but all involving the list being nil. For example, if my index page contains this:
<% @users.each do |user| %>
<li>
<%= link_to user.name, user %>
</li>
<% end %>
The server spits out:
undefined method `each' for nil:NilClass
Something like render @users
gives me the error of 'nil' is not an ActiveModel-compatible object that returns a valid partial path.
I am really confused why I would get this error if it works in the console. Let me know if you need some other code from me.
Thanks for your help!
Upvotes: 4
Views: 13845
Reputation: 2099
All this error messages indicate that @user is nil in your view. Assuming the view is views/users/index.html.erb, have you initialized @users in your users_controller index action like this?
def index
@users = User.all
end
Upvotes: 7