I have a users table. It contains a field “user_type”.
I added the following scope stmts to the user.rb file:
scope :uemployee, where(:user_type => 'employee')
scope :uclient, where(:user_type => 'client')
scope :ucontractor, where(:user_type => 'contractor')
I created a view and I would like it to list he employees.
Here is the code I’m trying to use:
<% @users.uemployee.each do |user| %>
But, I get “undefined method `uemployee’ for nil:NilClass”
What am I doing wrong?
Thanks
Looks like you wanted to do this:
But this is considered to be a bad practice. You have to prepare you data in a controller and a view just cycles through it:
But even this isn’t the best approach. If you create the file
views/users/_user.html.erbwhich shows the info about a particular user (current user will be available as a simpleuservariable, without the@) then you can simply write:then Rails will cycle through all the users “inside” the
@usersvariable and show them one-by-one.