# Page.replace\_html : How to update a DIV using a button

**URL:** <https://www.ruby-forum.com/t/page-replace-html-how-to-update-a-div-using-a-button/152084>\
**Category:** Rails\
**Created:** [October 29, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/page-replace-html-how-to-update-a-div-using-a-button/152084 "2008-10-29T23:36:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [October 29, 2008, 11:36pm UTC](https://www.ruby-forum.com/t/page-replace-html-how-to-update-a-div-using-a-button/152084/1 "2008-10-29T23:36:03Z")

</div>

I have this button which is supposed to update a DIV on the same page  
(using a partial). But it doesn’t.

What am I missing here ?!

# VIEW:

\<% form\_tag ‘/trees’ do -%\>  
\<%= submit\_to\_remote ‘new\_tree’, ‘Add a tree’, :url =\> { :controller  
=\> ‘trees’, :action =\> ‘new’ } %\>  
\<% end -%\>

# CONTROLLER

def new  
render :update do |page|  
page.replace\_html ‘newtree’, :partial =\> ‘user/trees/newtree’  
end  
end

Thank you very much for any help!  
Tom

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [October 30, 2008, 12:56am UTC](https://www.ruby-forum.com/t/page-replace-html-how-to-update-a-div-using-a-button/152084/2 "2008-10-30T00:56:15Z")

</div>

Your code seems right.  
But in case that I havn’t used submit\_to\_remote, so I cannot tell you  
what you are missing.  
Did you use debug to see whether your XMLHttpRequest call can ge  
caught by the ‘new’ action in controller?

However I can tell you that using form\_remote\_tag or defining a  
button\_to\_remote using remote\_function by yourself can do the trick.

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [October 30, 2008, 1:31am UTC](https://www.ruby-forum.com/t/page-replace-html-how-to-update-a-div-using-a-button/152084/3 "2008-10-30T01:31:54Z")

</div>

Hi Tom,

Tom Ha wrote:

> I have this button which is supposed to update a DIV  
> on the same page (using a partial). But it doesn’t.
> 
> What am I missing here ?!

Do you have Firebug installed? If not, you definitely need to get that.  
The console tab is invaluable in debugging Ajax.

> # VIEW:
> 
> \<% form\_tag ‘/trees’ do -%\>  
> \<%= submit\_to\_remote ‘new\_tree’, ‘Add a tree’, :url =\> { :controller  
> =\> ‘trees’, :action =\> ‘new’ } %\>  
> \<% end -%\>

This is interesting. I’m not sure what the impact would be of mixing  
form\_tag, which includes the url (’/trees’), with submit\_to\_remote,  
which  
also includes a url. Especially since they’re different urls in your  
case  
(a POST to trees is going to naturally invoke the ‘create’ action, not  
the  
‘new’ action. You might try just using form\_remote\_tag instead.

> # CONTROLLER
> 
> def new  
> render :update do |page|  
> page.replace\_html ‘newtree’, :partial =\> ‘user/trees/newtree’

You didn’t show it, so I’ll assume you have a

 element  
on  
the page, and that you have a newtree.rhtml file in views/user/trees/.  
If  
either of those is not true, your Ajax definitely won’t work.

Bottom line, though, is get Firebug installed in FireFox and see what  
it’s  
telling you.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![tharrisx](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e19adc/32.png) [@tharrisx](https://www.ruby-forum.com/u/tharrisx)\
**Post date:** [October 30, 2008, 8:25am UTC](https://www.ruby-forum.com/t/page-replace-html-how-to-update-a-div-using-a-button/152084/4 "2008-10-30T08:25:07Z")

</div>

Thanks for your hints!

In this case Firebug didn’t throw any error.

Sp I replaced:

\<% form\_tag ‘/trees’ do -%\>  
\<%= submit\_to\_remote ‘new\_tree’, ‘Add a tree’, :url =\> { :controller  
=\> ‘trees’, :action =\> ‘new’ } %\>  
\<% end -%\>

By:

\<%= button\_to\_function ‘Add a tree’ do |page| page.replace\_html  
‘newtree’, :partial =\> ‘user/trees/newtree’ end %\>

…and it works fine! Thanks!  
Tom
