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.
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?
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?
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.
�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| %>
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.
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.
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.