# Passing values from partial to controller

**URL:** <https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854>\
**Category:** Ruby\
**Created:** [May 19, 2010, 12:34pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854 "2010-05-19T12:34:57Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 12:34pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/1 "2010-05-19T12:34:57Z")

</div>

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

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

```

\<%= link\_to user.group\_name %\>

 \<%= custom\_button('Change') %\> 

```
        </p>
      </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>

```

## 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

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 12:53pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/2 "2010-05-19T12:53:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 1:07pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/3 "2010-05-19T13:07:14Z")

</div>

Gregor P. wrote:

> 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.

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 1:30pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/4 "2010-05-19T13:30:47Z")

</div>

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 D.:

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 1:42pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/5 "2010-05-19T13:42:24Z")

</div>

Gregor P. wrote:

> 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 D.:

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.

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/6 "2010-05-19T14:24:05Z")

</div>

Gregor P. wrote:

> yeah that’s because you pass only one parameter to the hidden\_field\_tag  
> so you got this:
> 
> 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
> 
> now you should get with params[:group\_name] the value that you set
> 
> hope this works 🙂

Thanks Gregor P… 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

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 2:04pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/7 "2010-05-19T14:04:09Z")

</div>

yeah that’s because you pass only one parameter to the hidden\_field\_tag  
so you got this:

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

now you should get with params[:group\_name] the value that you set

hope this works 🙂

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 2:53pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/8 "2010-05-19T14:53:20Z")

</div>

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 D.:

---

<div class="post-metadata">

**Author:** ![drk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@drk](https://www.ruby-forum.com/u/drk)\
**Post date:** [May 19, 2010, 3:34pm UTC](https://www.ruby-forum.com/t/passing-values-from-partial-to-controller/188854/9 "2010-05-19T15:34:08Z")

</div>

Gregor P. wrote:

> 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 D.:

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