nill object error

def grouplist

@currentgroupid=Group.find_by_group_name(params[:group_name])
@currentgroupfriends=GroupFriend.find_all_by_user_id_and_group_id(current_user.id,@currentgroupid)

format.html {redirect_to :back}

end

Please check my controller and help me in solving my problem.

I need to get groupid from the group_name from groups table and
The list of friendid s for in the group created by the user which are
available in the groupfriends table.
My requirement
Ex : select friends from groupfriends where userid=current_user.id and
groupid=currentgroupid

Please help me in writing the above query in the controller in rails.

Thanks.

Ravi Dtv wrote:

def grouplist

@currentgroupid=Group.find_by_group_name(params[:group_name])
@currentgroupfriends=GroupFriend.find_all_by_user_id_and_group_id(current_user.id,@currentgroupid)

format.html {redirect_to :back}

end

What does the log tell you is nil? current_user or @currentgroupid?

a. You're assuming that @currentgroupid will always have a value, which
may be okay depending on the source of params[:group_name], which we
don't know.

b. What is populating current_user? Is that the source of the nil
error?

Ar Chron wrote:

Ravi Dtv wrote:

def grouplist

@currentgroupid=Group.find_by_group_name(params[:group_name])
@currentgroupfriends=GroupFriend.find_all_by_user_id_and_group_id(current_user.id,@currentgroupid)

format.html {redirect_to :back}

end

What does the log tell you is nil? current_user or @currentgroupid?

a. You're assuming that @currentgroupid will always have a value, which
may be okay depending on the source of params[:group_name], which we
don't know.

b. What is populating current_user? Is that the source of the nil
error?

Thanks for your reply

def showfriendslist

@currentgroupid=Group.find_by_group_name(params[:group_name]).id
@friendsingroup=GroupFriend.find_all_by_user_id_and_group_id(current_user.id,
@currentgroupid)

respond_to do |format|
  format.html {render :partial => "friendlist" }
  format.xml

  end

This is my controller. My current_user is the current logged in user.
I can get the groupid value into @currentgroupid.

When I tried in console

@currentgroupid=Group.find_by_group_name('testinggroup')

=> #<Group id: 23, created_at: "2010-05-17 14:00:13", updated_at:
"2010-05-17 14:00:13", user_id: 9, group_name: "testinggroup">

@currentgroupid.id

=> 23

@friendsingroup=GroupFriend.find_all_by_user_id_and_group_id('9',@currentgroupid.id )

=> [#<GroupFriend id: 15, created_at: "2010-05-19 04:36:21", updated_at:
"2010-05-19 04:36:21", group_id: 23, friend_id: 4, user_id: 9>]

@friendsingroup.id

(irb):42: warning: Object#id will be deprecated; use Object#object_id
=> -618448038

@friendsingroup.group_id

NoMethodError: undefined method `group_id' for #<Array:0xb64676b4>
  from (irb):43

In view I get the following error

RuntimeError in UsersController#showfriendslist

Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id

Please help.. Im new to Ruby on Rails.

Thank you.

end

Ar Chron wrote:

Ravi Dtv wrote:

def grouplist

@currentgroupid=Group.find_by_group_name(params[:group_name])
@currentgroupfriends=GroupFriend.find_all_by_user_id_and_group_id(current_user.id,@currentgroupid)

format.html {redirect_to :back}

end

What does the log tell you is nil? current_user or @currentgroupid?

a. You're assuming that @currentgroupid will always have a value, which
may be okay depending on the source of params[:group_name], which we
don't know.

b. What is populating current_user? Is that the source of the nil
error?

Thanks for your reply

def showfriendslist

@currentgroupid=Group.find_by_group_name(params[:group_name]).id
@friendsingroup=GroupFriend.find_all_by_user_id_and_group_id(current_user.id,
@currentgroupid)

respond_to do |format|
format.html {render :partial => "friendlist" }
format.xml

end

This is my controller. My current_user is the current logged in user.
I can get the groupid value into @currentgroupid.

When I tried in console

@currentgroupid=Group.find_by_group_name('testinggroup')

=> #<Group id: 23, created_at: "2010-05-17 14:00:13", updated_at:
"2010-05-17 14:00:13", user_id: 9, group_name: "testinggroup">

@currentgroupid.id

=> 23

@friendsingroup=GroupFriend.find_all_by_user_id_and_group_id('9',@currentgroupid.id )

=> [#<GroupFriend id: 15, created_at: "2010-05-19 04:36:21", updated_at:
"2010-05-19 04:36:21", group_id: 23, friend_id: 4, user_id: 9>]

@friendsingroup.id

(irb):42: warning: Object#id will be deprecated; use Object#object_id
=> -618448038

@friendsingroup.group_id

NoMethodError: undefined method `group_id' for #<Array:0xb64676b4>
from (irb):43

That is because @friendsingroup is an array because you have used
find_all_by_... The clue is in the error message. It says that class
Array has not got a method group_id

In view I get the following error

RuntimeError in UsersController#showfriendslist

Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id

That means that you have used something.id where id is nil. You still
have not told us which line in the view is giving the error. Look at
the error message and see which line it is, then look to see what id
you are using on that line and most likely you will find the nil
object. Then you just have to work out why it is nil. In order to
avoid your view crashing you could test it for nil and put a message
in the view instead of allowing it to crash.

I also suggest also that you have a look at the Rails Guide on
debugging. It will show you ways of breaking in to your code to
inspect the variables. I use ruby-debug when I have this sort of
problem.

Colin

Colin Law wrote:

�format.xml
"2010-05-17 14:00:13", user_id: 9, group_name: "testinggroup">
�from (irb):43

That is because @friendsingroup is an array because you have used
find_all_by_... The clue is in the error message. It says that class
Array has not got a method group_id

In view I get the following error

�RuntimeError in UsersController#showfriendslist

Called id for nil, which would mistakenly be 4 -- if you really wanted
the id of nil, use object_id

That means that you have used something.id where id is nil. You still
have not told us which line in the view is giving the error. Look at
the error message and see which line it is, then look to see what id
you are using on that line and most likely you will find the nil
object. Then you just have to work out why it is nil. In order to
avoid your view crashing you could test it for nil and put a message
in the view instead of allowing it to crash.

I also suggest also that you have a look at the Rails Guide on
debugging. It will show you ways of breaking in to your code to
inspect the variables. I use ruby-debug when I have this sort of
problem.

Colin

Thanks Colin for the reply.

When debugging i find currentgroupid is nill.

How can I solve this. Possible solutions to pass values form partial to
controller.
        <% @groups.each do |g| %>
   <p>
<h5>
     <%= link_to_remote g.group_name, :update => "abc", :url => {
:controller =>
"users" , :action => "showfriendslist" } %>

   <%= hidden_field_tag :group_name %>
</h5>
</p>
<% end %>

I can see my groupname displaying in my page, but when I click on my
groupname link, Im not able to pass groupname to my controller.

My controller:

def showfriendslist

   @currentgroupid=Group.find_by_group_name(params[:group_name]).id

#@friendsingroup=GroupFriend.find_all_by_user_id_and_group_id(current_user.id,@currentgroupid)

  respond_to do |format|
  format.html {render :partial => "friendlist" }

  format.xml
  end

end

Please help. Thank you.

...>
When debugging i find currentgroupid is nill.

How can I solve this. Possible solutions to pass values form partial to
controller.
<% @groups.each do |g| %>
<p>
<h5>
<%= link_to_remote g.group_name, :update => "abc", :url => {
:controller =>
"users" , :action => "showfriendslist" } %>

<%= hidden_field_tag :group_name %>
</h5>
</p>
<% end %>

I can see my groupname displaying in my page, but when I click on my
groupname link, Im not able to pass groupname to my controller.

Using a hidden field does not work as you are not in a form. That is
how to do it if you were submitting from a form. Instead just include
the name in the url - something like
<%= link_to_remote g.group_name, :update => "abc", :url => {
:controller => "users" , :action => "showfriendslist", :group_name =>
g.group_name } %>

Look at the html generated by this to check that it is being included correctly.

Colin

Colin Law wrote:

I can see my groupname displaying in my page, but when I click on my
groupname link, Im not able to pass �groupname to my controller.

Using a hidden field does not work as you are not in a form. That is
how to do it if you were submitting from a form. Instead just include
the name in the url - something like
<%= link_to_remote g.group_name, :update => "abc", :url => {
:controller => "users" , :action => "showfriendslist", :group_name =>
g.group_name } %>

Look at the html generated by this to check that it is being included
correctly.

Colin

Thank you Colin

I got the output.

Can you please solve this error for me

undefined method `each' for 4:Fixnum

I am sure you can work it out for yourself. Look at the line of code
that generates the error, presumably there is a call to 'each' there.
The error says that you are trying to call each for the value 4 (which
is of type Fixnum), presumably you expected it to be an Array. If you
can't see the problem by code inspection use ruby-debug to break in
before that line and have a look at the variable you are calling each
on.

Colin