Ruby On Rails - "undefined method `id' for 4:Fixnum"

20.7k views Asked by At

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.

4

There are 4 answers

3
sepp2k On BEST ANSWER

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.

4
Gerry On

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!

0
Jigar Bhatt On

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.

0
Dhanshree On

Try this, it worked for me.

@item.unit_of_measure.name 

instead of

@item.unit_of_measure_id.name