I have a view template “list_users.rhtml”. On one line, I have
<% @users = User.find(:all) %>
in my models directory I have a file user.rb which starts with the line
class User < ActiveRecord::Base
Problem: When I invoke list_user.rhtml, I’m getting the error
“Uninitialized constant User” which points at the User.find line listed
above. It’s my understanding that find is built into the ActiveRecord
class, so I think this is implying that it doesn’t see my class. Why?
What else might I need to do to make sure my model file gets loaded?
On Sat, 2006-10-07 at 19:18 +0200, Michael S. wrote:
above. It’s my understanding that find is built into the ActiveRecord
class, so I think this is implying that it doesn’t see my class. Why?
What else might I need to do to make sure my model file gets loaded?
you model class is fine the way it is.
in app/controllers/UserController.rb you should have a method that has
the same name as the template you want rails to automagically call. This
method needs to pass the template a class variable (preceded by a ‘@’)
the template can have access to:
def list_users # same name as template @all_users = User.find(:all) # note @ before var name
end # no need to invoke anything
then in app/views/users/list_users.rhtml you can access the collection
of users via the class variable @all_users
example:
<% foreach user in @all_users %>
<%= user.name
<% end %>
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.