views:

23

answers:

1

Hi,

I am very new to Rails. I am starting my learning process by first listing all the users.

FIrst i had added a action in the controller as

def list_users
  @users=User.find(:all)
end

And in the View users/list_users.html.erb i have added the line

list_users.html.erb:

<%= Time.now %>

<% @users.each do |user| %>
  <%= user.firstname %>
<% end %>

And for routing i have added the routes as

map.list_users '/list_users', :controller => 'users', :action => 'list_users'

Thats it .. when i run my app , its showing me the error as

     Development mode eh? Here is the error - #<ActionView::TemplateError: 
     ActionView::TemplateError (You have a nil object when you didn't expect it!
      You might have expected an instance of Array.
      The error occurred while evaluating nil.each) on line #7 of app/views/users/list_users.html.erb:

Why so ?? Please give some suggestions ..

Solution :

I myself find that the controller action is under protected , thats why it showed me the error. Sorry for asking silly question THanks for everyone 's help .

+1  A: 

It seems that you didn't use the login method in list_users.html.erb, where did you used it?

But here is another error (maybe typo error only?)

<% @users.each do |user| %>
  <%= user.firstname # not @user here! %>
<% end %>
PeterWong
Still i am getting the same error..You might have expected an instance of Array.The error occurred while evaluating nil.each) on line #7 of app/views/users/list_users.html.erb: -- EVEN AFTER CHANGING THE @user to user.firstname..
Aruna
So it's complaining `@users` is null...Could you provide more codes in your controller and you view?
PeterWong