I recently decided I wanted to list all the users
in my Ruby On Rails application - since I couldn't figure out how to list them any other way, I decided to use partials.
I have the following on my administration page (just hooked up to a its own administration controller):
<%= render :partial => User.find(:all) %>
I then have a file called _user.html.erb
in my users view folder. This contains the following:
<ul>
<% div_for @user.object_id do %>
<li><%= link_to user.username, user.username %></li>
<% end %>
</ul>
When the application runs and I go to the administration page, I get the following error:
undefined method `id' for 4:Fixnum
It says it's because of this line (which is in the partial file):
<% div_for @user.object_id do %>
I'm unsure why this happens (and have googled for hours to try and find results and only find solutions that don't work for me). I think it's something to do with my usage of the @user
instance variable, but I'm not totally sure.
You get that error because div_for
expects an active record object as an argument, which it calls the id
method on. You pass in a fixnum (the result of @user.object_id
), which is not an active record object and does not have an id
method.
So pass in @user
instead of @user.object_id
and it will work.
Also you should use user
instead of @user
, rails 3 does not set instance variables for partials anymore.