开发者

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

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开发者_如何学JAVA>

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.


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!


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.


Try this, it worked for me.

@item.unit_of_measure.name 

instead of

@item.unit_of_measure_id.name
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜