# What does :with do?

**URL:** <https://www.ruby-forum.com/t/what-does-with-do/75727>\
**Category:** Rails\
**Created:** [November 12, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/what-does-with-do/75727 "2006-11-12T20:47:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 12, 2006, 8:47pm UTC](https://www.ruby-forum.com/t/what-does-with-do/75727/1 "2006-11-12T20:47:00Z")

</div>

Railsers:

What does :with do in \_remote calls, like link\_to\_remote() or  
periodically\_call\_remote()?

The documentation says :with should point to:

‘A JavaScript expression specifying the parameters for the  
XMLHttpRequest. This defaults to ‘value’, which in the evaluated  
context refers to the new field value. If you specify a string without  
a “=”, it’ll be extended to mean the form key that the value should be  
assigned to. So :with =\> “term” gives “‘term’=value”. If a “=” is  
present, no extension will happen.’

What does all that mean??

I need to take the value from a hidden field and pass this with the  
\_remote call. Is that what with is for? When I naively turn it on  
(with the name of the hidden field as a string) the  
periodically\_call\_remote() stops working.

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 13, 2006, 3:33am UTC](https://www.ruby-forum.com/t/what-does-with-do/75727/2 "2006-11-13T03:33:49Z")

</div>

Phlip,

> I need to take the value from a hidden field and pass this with the  
> \_remote call. Is that what with is for?

Yes. Are you passing the _id_ of the hidden filed in :with? If that  
doesn’t  
work, try:

:with =\> “Form.Element.serialize(‘id\_of\_hidden\_field’)”,  
:method =\> ‘get’

Note that the :method needs to be set to ‘get’ (case sensitive) or it  
won’t  
work.

Cheers

Hammed

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 13, 2006, 5:41am UTC](https://www.ruby-forum.com/t/what-does-with-do/75727/3 "2006-11-13T05:41:58Z")

</div>

Hammed M. wrote:

> :with =\> “Form.Element.serialize('id\_of\_hidden\_field')”,

Thanks!

Someone had to tell me “the remote caller will evaluate the :with  
target as javascript, hence it can pull live data from your DOM.”

:with =\> ‘id\_of\_hidden\_field’ didn’t work, and I suppose I could have  
done something like ‘document.all.id\_of.value’. But your scriptalicious  
Form.Element.serialize() worked.

I’m not sure why it puts a “\_”=\>nil into the params, though, but that’s  
too small to be worth optimizing away!

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!!

---

<div class="post-metadata">

**Author:** ![Phlip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ed8c4c/32.png) [@Phlip](https://www.ruby-forum.com/u/Phlip)\
**Post date:** [November 13, 2006, 8:26am UTC](https://www.ruby-forum.com/t/what-does-with-do/75727/4 "2006-11-13T08:26:12Z")

</div>

You should be able to do something like

:with =\> “‘name=’ + escape($F(‘hidden\_id’))”

in your remote to pass on the value of a hidden field with an ID of  
“hidden\_id” with your \_remote call. It will be available in your method  
as params[‘name’].

For example:

\<%= link\_to\_remote ‘click here’,  
{:update =\> ‘some\_element’,  
:with =\> “‘name=’ + escape($F(‘hidden\_id’))”},  
:url =\> {:action =\> ‘my\_method’} -%\>

David
