# :with =\> javascript method call

**URL:** <https://www.ruby-forum.com/t/with-javascript-method-call/139972>\
**Category:** Rails\
**Created:** [June 9, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972 "2008-06-09T18:43:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 9, 2008, 6:43pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972/1 "2008-06-09T18:43:46Z")

</div>

I have a form and I am trying to pass a next\_question param which will  
hold a value that is returned by a javascript function. Is this  
possible? I am new to using :with and javascript and have read  
documentation on it as well as an entire page explaining it with  
examples but I still dont really understand how to do this, or if it is  
even possible.

:with =\> “'next\_question='javascript: getNextQuestion()”

Thanks in advance,

chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 9, 2008, 6:57pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972/2 "2008-06-09T18:57:44Z")

</div>

Chris H. wrote:

> I have a form and I am trying to pass a next\_question param which will  
> hold a value that is returned by a javascript function. Is this  
> possible? I am new to using :with and javascript and have read  
> documentation on it as well as an entire page explaining it with  
> examples but I still dont really understand how to do this, or if it is  
> even possible.
> 
> :with =\> “'next\_question='javascript: getNextQuestion()”
> 
> Thanks in advance,
> 
> chris

What about the :compete call back. When using form\_remote\_tag I use  
:complete to clear a text\_field for example. I imagine if you had a div  
with an id you could use that javaScript function to change the  
innerText of that div upon complete to add the next question. Hope this  
helps,

-S

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 9, 2008, 7:00pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972/3 "2008-06-09T19:00:19Z")

</div>

On 9 Jun 2008, at 17:43, Chris H. wrote:

> :with =\> “'next\_question='javascript: getNextQuestion()”

I’ve got some examples here:

> **[:with or :without you: link\_to\_remote's mysterious parameter - Space Vatican](https://www.spacevatican.org/2008/5/17/with-or-without-you-link_to_remote-s-mysterious-parameter/)**
>
> One of the nice things about Rails is that it makes it really easy to get off the ground with Ajax, thanks to the link\_to\_remote helper (and all the …

it is possible - what you don’t seem to have grasped yet is that the  
entirety of the :with parameter should be a piece of javascript code.  
Fred

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 9, 2008, 7:19pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972/4 "2008-06-09T19:19:05Z")

</div>

On 9 Jun 2008, at 18:07, Chris H. wrote:

> :with =\> “‘next\_question=’+getNextQuestion()”

The problem is probably the getNextQuestion is not returning something  
that is uri encoded (or not returning anything at all). Remember that  
in javascript you need an explicit return statement, and use  
encodeURIComponent.

Fred

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 9, 2008, 7:40pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972/5 "2008-06-09T19:40:09Z")

</div>

One last question and then I am giving up. It seems like nothing is  
getting passed over at all. The following should pass next\_question to  
the controller with a value of 6. In the controller I should be able to  
access it by saying params[:free\_response][:next\_question] correct?

\<% form\_for(:free\_response, :url =\> {:action =\> ‘next’, :question =\>  
@question.id}, :with =\> “‘next\_question=’+encodeURIComponent(6)”) do  
|form| %\>

Sorry for all the questions I am just completely lost on this one. I  
tried the encodeURIComponent() before and nothing changed so I kept  
changing it around.

Chris

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 9, 2008, 7:07pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972/6 "2008-06-09T19:07:47Z")

</div>

Fred,

I’m sorry, that was the page I was reading. I found a link to it in  
another forum. I have tried several different ways of doing this and  
none of them seem to be working for me. From one of the last 2 examples  
on your page it seems like the following should work but I am not having  
any luck.

:with =\> “‘next\_question=’+getNextQuestion()”

I am glad to hear this is possible though.

Thanks

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 9, 2008, 7:46pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972/7 "2008-06-09T19:46:10Z")

</div>

On 9 Jun 2008, at 18:40, Chris H. wrote:

> |form| %\>
> 
> Sorry for all the questions I am just completely lost on this one. I  
> tried the encodeURIComponent() before and nothing changed so I kept  
> changing it around.

:with is only for remote\_form\_for, link\_to\_remote etc… in your case  
i’d probably use an onsubmit to update a hidden field

Fred

---

<div class="post-metadata">

**Author:** ![bigdaddy71](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bbe5ce/32.png) [@bigdaddy71](https://www.ruby-forum.com/u/bigdaddy71)\
**Post date:** [June 9, 2008, 7:57pm UTC](https://www.ruby-forum.com/t/with-javascript-method-call/139972/8 "2008-06-09T19:57:18Z")

</div>

Thanks for your time and patience.
