# Using form\_remote\_tag in an helper

**URL:** <https://www.ruby-forum.com/t/using-form-remote-tag-in-an-helper/116433>\
**Category:** Rails\
**Created:** [October 5, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/using-form-remote-tag-in-an-helper/116433 "2007-10-05T14:41:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [October 5, 2007, 2:41pm UTC](https://www.ruby-forum.com/t/using-form-remote-tag-in-an-helper/116433/1 "2007-10-05T14:41:26Z")

</div>

Hello,

On one of my pages I’ve got a button which should do various stuff based  
on some params passed to the page (and some retrieved from the session).

I was doing it fine using an helper :

def action\_button product  
if logged\_in?  
button\_to “Already added!”,{:action=\>:show, :id=\>product},  
elsif @cart.items.include? product  
button\_to “Already  
added!”,{:controller=\>‘store’},{:disabled=\>true}  
else  
button\_to “Add to cart”, {:action=\>:add\_to\_cart, :id=\>product}  
end  
end

But now I’d like to start using some eye-candy effects with Ajax. So I  
tried to write down this :

def action\_button product  
if logged\_in?  
button\_to “Already added!”,{:action=\>:show, :id=\>product},  
elsif @cart.items.include? product  
button\_to “Already  
added!”,{:controller=\>‘store’},{:disabled=\>true}  
else  
to\_return = ‘’  
form\_remote\_tag :url =\> {:action=\>:add\_to\_cart,:id=\>product} do  
to\_return += submit\_tag “Add to cart”  
end  
to\_return  
end  
end

But it didn’t work. Turns out that “form\_remote\_tag” needs to call  
\_erbout which is only defined in the view, but not in the helpers … Do  
you know how I could fix my problem (without putting too much stuff in  
my views) ?

Thanks a lot

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [October 5, 2007, 7:04pm UTC](https://www.ruby-forum.com/t/using-form-remote-tag-in-an-helper/116433/2 "2007-10-05T19:04:55Z")

</div>

On 10/5/07, Stefano G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> else
> 
> ```
> 
> you know how I could fix my problem (without putting too much stuff in  
> my views) ?

Don’t use a block with form\_remote\_tag in this case. You want  
something like this:

form\_remote\_tag(:url =\> …blah…) + submit\_tag(‘Add to cart’) +  
end\_form\_tag

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [October 6, 2007, 3:18pm UTC](https://www.ruby-forum.com/t/using-form-remote-tag-in-an-helper/116433/3 "2007-10-06T15:18:41Z")

</div>

Thanks, it works perfectly

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [January 9, 2008, 6:00am UTC](https://www.ruby-forum.com/t/using-form-remote-tag-in-an-helper/116433/4 "2008-01-09T06:00:16Z")

</div>

Any ideas on how to do this now that end\_form\_tag is deprecated?

Thanks!

On Oct 6 2007, 6:18 am, Stefano G. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![esox\_ch](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/4da419/32.png) [@esox\_ch](https://www.ruby-forum.com/u/esox_ch)\
**Post date:** [January 30, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/using-form-remote-tag-in-an-helper/116433/5 "2008-01-30T17:18:35Z")

</div>

bhbrinckerhoff wrote:

> Any ideas on how to do this now that end\_form\_tag is deprecated?
> 
> Thanks!
> 
> On Oct 6 2007, 6:18 am, Stefano G. \<rails-mailing-l…@andreas-

Hi,

I have a scenario wherein i have opened a javascript popup window, now i  
want the user to do some actions in that window, and now the form from  
the window is submited, on submit i call a method that does the server  
side handling and then on success i call the window.close and now on  
complete i am trying to invoke another ajax call to update a dom element  
in the main window.  
My code looks like this  
\<%= form\_remote\_tag :method=\>‘post’, :url=\>{:action =\>‘add\_from\_pop\_up’,  
:count =\> count},  
:success =\> “window.close();”,  
:complete =\> “javascript:test(count)”%\>

and the javascript fcn is  
function test(count)  
{  
qs = “count=”+ count  
new Ajax.Request(’/reporting/update\_div’, {parameters:qs,  
asynchronous:true, evalScripts:true});  
return false;  
}

Any suggestions on how to invoke this javascript method to update a DOM  
element.

Neha
