passing values from partial to controller

R

Ravi Dtv

I am trying to pass login and group_name from partial to controller, but
I dont get the values.

I see the following error:
RuntimeError in UsersController#addfriendtogroups

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

I tried in the console, where I get the result. But, in application I
cannot pass the values.



html page
-----------------

<% @groups.each do |user| %>
<%= render :partial => 'users/grouprow', :locals => {:user
=> user}%>

<% end %>

Partial
-----------
<tr>
<td class="people_profile">
<div class="clear_float_effect">
<div class="profile_block">

<p>
<span title="Click to edit" id="edit_location">

<%= link_to user.group_name %>

</span>
<a href="#" class="short_description_text btn"
id="edit_group_link"> <%= custom_button('Change') %> </a>



</p>
</div>

<div>
<% form_tag :)controller => "users" , :action => "addfriendtogroups")
do %>

Add friend:
<%= text_field_with_auto_complete :user, 'login', {:size =>
20}, {:tokens => ','} %>

<%= submit_tag "Add to group", :class =>
"submit_input_button rounded_corner" %>
<% end %>
</div>

</div>
</td>

</tr>



Controller
----------------

def addfriendtogroups
@f=User.find_by_login(params[:login]).id
@g=Group.find_by_group_name(params[:group_name]).id

@addfriend=GroupFriend.create:)user_id => current_user.id ,
:group_id => @g,:friend_id => @f)

respond_to do |format|

if @addfriend.save

flash[:notice] = "Friend added to group successfully."
format.html {redirect_to :back}

end

end
end
 
R

Ravi Dtv

Gregor said:
hi,

pass your parameters to your partial like this:
<%=render :partial => 'users/grouprow' ,:collection => @groups%>


and when you want to call the parameters in your partial, you have to
call it by the name of your partial so it will be something like this:

grouprow.id
grouprow.name


Greg

Thanks Greg for the reply.

I can display the values in my partial.
My problem is I need to pass the values from my partial to the
controller.

I have a link, textbox and button in my partial.
The link holds groupname. the textbox holds friendname.
When I click Add button, the friend in the textbox should be added to
the group and insert into the table.
My def in controller

Controller
----------------

def addfriendtogroups
@f=User.find_by_login(params[:login]).id
@g=Group.find_by_group_name(params[:group_name]).id

@addfriend=GroupFriend.create:)user_id => current_user.id ,
:group_id => @g,:friend_id => @f)

respond_to do |format|

if @addfriend.save

flash[:notice] = "Friend added to group successfully."
format.html {redirect_to :back}

end

end
end


Please correct the def.

Thanks.
 
G

Gregor Panek

Hi,

I think your problem is this line:
@g=3DGroup.find_by_group_name(params[:group_name]).id

you want to pass the params :group_name which are nil because in your =
form you don't pass the :group_name to your controller:

<% form_tag :)controller =3D> "users" , :action =3D> =
"addfriendtogroups")
do %>

Add friend:
<%=3D text_field_with_auto_complete :user, 'login', {:size =3D>
20}, {:tokens =3D> ','} %>

<%=3D submit_tag "Add to group", :class =3D>
"submit_input_button rounded_corner" %>
<% end %>

So pass your :group_name trough a hidden field to your controller

hidden_field=20


Am 19.05.2010 um 13:07 schrieb Ravi Dtv:
=20
Thanks Greg for the reply.
=20
I can display the values in my partial.
My problem is I need to pass the values from my partial to the=20
controller.
=20
I have a link, textbox and button in my partial.
The link holds groupname. the textbox holds friendname.
When I click Add button, the friend in the textbox should be added to=20=
the group and insert into the table.
My def in controller
=20
Controller
----------------
=20
def addfriendtogroups
@f=3DUser.find_by_login(params[:login]).id
@g=3DGroup.find_by_group_name(params[:group_name]).id
=20
@addfriend=3DGroupFriend.create:)user_id =3D> current_user.id ,
:group_id =3D> @g,:friend_id =3D> @f)
=20
respond_to do |format|
=20
if @addfriend.save
=20
flash[:notice] =3D "Friend added to group successfully."
format.html {redirect_to :back}
=20
end
=20
end
end
=20
=20
Please correct the def.
=20
Thanks.
--=20
Posted via http://www.ruby-forum.com/.
=20
 
R

Ravi Dtv

Gregor said:
Hi,

I think your problem is this line:
@g=Group.find_by_group_name(params[:group_name]).id

you want to pass the params :group_name which are nil because in your
form you don't pass the :group_name to your controller:

<% form_tag :)controller => "users" , :action => "addfriendtogroups")
do %>

Add friend:
<%= text_field_with_auto_complete :user, 'login', {:size =>
20}, {:tokens => ','} %>

<%= submit_tag "Add to group", :class =>
"submit_input_button rounded_corner" %>
<% end %>

So pass your :group_name trough a hidden field to your controller

hidden_field


Am 19.05.2010 um 13:07 schrieb Ravi Dtv:




I tried with a hidden field
<% form_tag :)controller => "users" , :action => "addfriendtogroups") do
%>

Add friend:
<%= text_field_with_auto_complete :user, 'login', {:size => 20},
{:tokens => ','} %>
<%= hidden_field_tag :group_name %>
<%= submit_tag "Add to group", :class =>
"submit_input_button rounded_corner" %>
<% end %>

but it still a nil value.

The error :



RuntimeError in UsersController#addfriendtogroups

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


Request

Parameters:

{"group_name"=>"",
"commit"=>"Add to group",
"authenticity_token"=>"MHB8Ys8vccsGwbgnsb9r5T68oqirPpW6YUscfDojZhY=",
"id"=>"testing",
"user"=>{"login"=>"santosh"}}

where 'testing' is current user login, santosh is friend, and my
group_name is passing null value.
 
G

Gregor Panek

yeah that's because you pass only one parameter to the hidden_field_tag =
so you got this:
<input id=3D"tags_list" name=3D"tags_list" type=3D"hidden" />

and your value is still nil, so you have to pass a second parameter to =
the hidden_field_tag to set the value:

<%=3Dhidden_field_tag :group_name, "yourgroupname"%>
then you got this in html

<input id=3D"token" name=3D"token" type=3D"hidden" value=3D"yourgroupname"=
/>
now you should get with params[:group_name] the value that you set=20

hope this works :)=20

Gregor said:
Hi,
=20
I think your problem is this line:
@g=3DGroup.find_by_group_name(params[:group_name]).id
=20
you want to pass the params :group_name which are nil because in your=20=
form you don't pass the :group_name to your controller:
=20
<% form_tag :)controller =3D> "users" , :action =3D> = "addfriendtogroups")
do %>
=20
Add friend:
<%=3D text_field_with_auto_complete :user, 'login', {:size =3D>
20}, {:tokens =3D> ','} %>
=20
<%=3D submit_tag "Add to group", :class =3D>
"submit_input_button rounded_corner" %>
<% end %>
=20
So pass your :group_name trough a hidden field to your controller
=20
hidden_field
=20
=20
Am 19.05.2010 um 13:07 schrieb Ravi Dtv:
=20
=20
=20
=20
I tried with a hidden field
<% form_tag :)controller =3D> "users" , :action =3D> = "addfriendtogroups") do=20
%>
=20
Add friend:
<%=3D text_field_with_auto_complete :user, 'login', {:size =3D> = 20},=20
{:tokens =3D> ','} %>
<%=3D hidden_field_tag :group_name %>
<%=3D submit_tag "Add to group", :class =3D>=20
"submit_input_button rounded_corner" %>
<% end %>
=20
but it still a nil value.
=20
The error :
=20
=20
=20
RuntimeError in UsersController#addfriendtogroups
=20
Called id for nil, which would mistakenly be 4 -- if you really wanted=20=
 
R

Ravi Dtv

Gregor said:
yeah that's because you pass only one parameter to the hidden_field_tag
so you got this:
<input id="tags_list" name="tags_list" type="hidden" />

and your value is still nil, so you have to pass a second parameter to
the hidden_field_tag to set the value:

<%=hidden_field_tag :group_name, "yourgroupname"%>
then you got this in html

<input id="token" name="token" type="hidden" value="yourgroupname" />
now you should get with params[:group_name] the value that you set

hope this works :)

Thanks Gregor Panek. I am able to pass the values but plz check my
action in the controller.
RuntimeError in UsersController#addfriendtogroups

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

Request

Parameters:

{"group_name"=>"testinggroup2",
"commit"=>"Add to group",
"authenticity_token"=>"MHB8Ys8vccsGwbgnsb9r5T68oqirPpW6YUscfDojZhY=",
"id"=>"testing",
"user"=>{"login"=>"santosh"}}


I need to get the id from login( santosh) and id from Group
(testinggroup2)
and insert the both ids into another table.


Please help. Thank you.

def addfriendtogroups
@f=User.find_by_login(params[:login]).id
@g=Group.find_by_group_name(params[:group_name]).id

@addfriend=GroupFriend.create:)user_id => current_user.id ,
:group_id => @g,:friend_id => @f)

respond_to do |format|

if @addfriend.save

flash[:notice] = "Friend added to group successfully."
format.html {redirect_to :back}

end

end
end
 
G

Gregor Panek

I can't see an error in your controller,
the only think i see ist that you got for your username this parameter:
"user"=3D>{"login"=3D>"santosh"}

so what you can probably try is to change your input type into this:
<%=3D text_field_with_auto_complete :user {:size =3D>
20}, {:tokens =3D> ','} %>

and your controller in this
@f=3DUser.find_by_login(params[:user]).id

i hope that your column login in the Model User is expecting a String =
with the Name and not an id.=20




Am 19.05.2010 um 14:24 schrieb Ravi Dtv:
Gregor said:
yeah that's because you pass only one parameter to the = hidden_field_tag=20
so you got this:
<input id=3D"tags_list" name=3D"tags_list" type=3D"hidden" />
=20
and your value is still nil, so you have to pass a second parameter = to=20
the hidden_field_tag to set the value:
=20
<%=3Dhidden_field_tag :group_name, "yourgroupname"%>
then you got this in html
=20
<input id=3D"token" name=3D"token" type=3D"hidden" = value=3D"yourgroupname" />
now you should get with params[:group_name] the value that you set
=20
hope this works :)
=20
Thanks Gregor Panek. I am able to pass the values but plz check my=20
action in the controller.
RuntimeError in UsersController#addfriendtogroups
=20
Called id for nil, which would mistakenly be 4 -- if you really wanted=20=
the id of nil, use object_id
=20
Request
=20
Parameters:
=20
{"group_name"=3D>"testinggroup2",
"commit"=3D>"Add to group",
"authenticity_token"=3D>"MHB8Ys8vccsGwbgnsb9r5T68oqirPpW6YUscfDojZhY=3D"= ,
"id"=3D>"testing",
"user"=3D>{"login"=3D>"santosh"}}
=20
=20
I need to get the id from login( santosh) and id from Group=20
(testinggroup2)
and insert the both ids into another table.
=20
=20
Please help. Thank you.
=20
def addfriendtogroups
@f=3DUser.find_by_login(params[:login]).id
@g=3DGroup.find_by_group_name(params[:group_name]).id
=20
@addfriend=3DGroupFriend.create:)user_id =3D> current_user.id ,
:group_id =3D> @g,:friend_id =3D> @f)
=20
respond_to do |format|
=20
if @addfriend.save
=20
flash[:notice] =3D "Friend added to group successfully."
format.html {redirect_to :back}
=20
end
=20
end
end
=20
=20
--=20
Posted via http://www.ruby-forum.com/.
=20
 
R

Ravi Dtv

Gregor said:
I can't see an error in your controller,
the only think i see ist that you got for your username this parameter:
"user"=>{"login"=>"santosh"}

so what you can probably try is to change your input type into this:
<%= text_field_with_auto_complete :user {:size =>
20}, {:tokens => ','} %>

and your controller in this
@f=User.find_by_login(params[:user]).id

i hope that your column login in the Model User is expecting a String
with the Name and not an id.




Am 19.05.2010 um 14:24 schrieb Ravi Dtv:

Still the same error, Panek.
It was not solved.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top