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

4

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.

Diggins answered 9/2, 2011 at 21:13 Comment(0)
A
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.

Autoionization answered 9/2, 2011 at 21:17 Comment(3)
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"Diggins
@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).Autoionization
Thanks so much! Using "user" instead of "@user" works a treat! :DDiggins
S
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!

Siamese answered 9/2, 2011 at 21:20 Comment(4)
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"Diggins
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 :)Siamese
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)Diggins
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 renderSiamese
A
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.

Androgynous answered 20/12, 2014 at 12:2 Comment(0)
S
0

Try this, it worked for me.

@item.unit_of_measure.name 

instead of

@item.unit_of_measure_id.name
Swear answered 27/11, 2017 at 8:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.