5

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.

intcreator
  • 4,206
  • 4
  • 21
  • 39

4 Answers4

8

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.

sepp2k
  • 363,768
  • 54
  • 674
  • 675
  • Using @user I get the error: "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id" –  Feb 09 '11 at 21:26
  • @Joesavage1: If you're using rails 3, you need to use `user` instead of `@user` (and if you aren't, you should use `user` anyway because that way it will work with any rails version). – sepp2k Feb 09 '11 at 21:34
  • 1
    Thanks so much! Using "user" instead of "@user" works a treat! :D –  Feb 10 '11 at 07:18
0

instead of object you are using it's column or visa varsa you are using at that time you will get this kind of error.

Example

I am using id instead of user = User.first object.

Jigar Bhatt
  • 4,217
  • 2
  • 34
  • 42
0

Try this, it worked for me.

@item.unit_of_measure.name 

instead of

@item.unit_of_measure_id.name
Vega
  • 27,856
  • 27
  • 95
  • 103
Dhanshree
  • 71
  • 9
0

Lose the .object_id part. I seriously can't think why are you using object_id!Do you have a good reason for doing so? Anyway div_for wraps a div around an object, so leave the .object_id part!

Gerry
  • 5,326
  • 1
  • 23
  • 33
  • Using @user I get the error: "Called id for nil, which would mistakenly be 4 -- if you really wanted the id of nil, use object_id" –  Feb 09 '11 at 21:26
  • The @user then is nil. Check your controller again to set the @user instance variable. I guess that you don't want the @user to be nil :) – Gerry Feb 09 '11 at 21:35
  • Since it's the user partial - I assumed that it already knows what @user is. Since I want to loop through the users, surely it should already know what each @user is? If I need to add something in a controller - what controller (users, or administrate) –  Feb 10 '11 at 07:15
  • No, if you haven't defined the @user in your controller the view / partial can't (and shouldn't) guess the @user. Answer the following: 1. In what controller action your view is linked? 2. Are you using resources? 3. Have you tried render :partial => "your_partial", :object => @user ? This tells the partial with which object must render – Gerry Feb 10 '11 at 08:12