# Making Ajax rerender in div

**URL:** <https://www.ruby-forum.com/t/making-ajax-rerender-in-div/75693>\
**Category:** Rails\
**Created:** [November 12, 2006, 12:29pm UTC](https://www.ruby-forum.com/t/making-ajax-rerender-in-div/75693 "2006-11-12T12:29:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [November 12, 2006, 12:29pm UTC](https://www.ruby-forum.com/t/making-ajax-rerender-in-div/75693/1 "2006-11-12T12:29:36Z")

</div>

In Onlamp there is a wonderful introduction to Ajax under Rails in  
“Ajax on Rails,” (  
[Radar – O’Reilly](http://www.onlamp.com/pub/a/onlamp/2005/06/09/rails_ajax.html) ).

I have taken the Author, Curt H.’ code from the example, and in  
looking at other examples, a common seeming problem (to me) keeps  
recurring.

If I want to render to a \< div \> element, when I make a subsequent  
render to it, it then shows both this rendering as well as the previous  
– when in fact, I only want the latest rendering. The code for an  
rhtml as well as a method coinciding with that rhtml bears this out.  
How can I amend such code so as to only print the last link\_to\_remote  
result? Also, would there be a way to render to, say, a textbox in this  
example?

Thank you. Ive looked at other Ajax on Rails examples and they all seem  
to have this same ‘problem,’ which I’d like to get around.

 Ajax Demo \<%= javascript\_include\_tag :defaults %\> 
# What time is it?

 I don't have the time, but \<%= link\_to\_remote( "click here", :update =\> "time\_div", :url =\> { :action =\> :say\_when }, :position =\> "bottom" ) %\> and I will look it up. 

def say\_when  
render\_text “

The time is **” + DateTime.now.to\_s + “**

”  
end

---

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [November 12, 2006, 3:07pm UTC](https://www.ruby-forum.com/t/making-ajax-rerender-in-div/75693/2 "2006-11-12T15:07:55Z")

</div>

On Nov 12, 2006, at 5:27 AM, Ike wrote:

> Thank you. Ive looked at other Ajax on Rails examples and they all
> 
> def say\_when  
> render\_text "
> 
> The time is **" + DateTime.now.to\_s + “** \</  
> p\>”  
> end
> 
> Try changing the code to render a partial. Also, don’t use #render,  
> use #replace\_html. Try this:
> 
> def say\_when  
> replace\_html “time\_div”,  
> :partial =\> “time”,  
> end
> 
> The problem here will be the entire div will get replaced by whatever  
> HTML is in the partial. The “I don’t have the time…” text will all  
> get blown away so the user will only get to click on this once.
> 
> cr

---

<div class="post-metadata">

**Author:** ![ike](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/df705f/32.png) [@ike](https://www.ruby-forum.com/u/ike)\
**Post date:** [November 12, 2006, 3:28pm UTC](https://www.ruby-forum.com/t/making-ajax-rerender-in-div/75693/3 "2006-11-12T15:28:12Z")

</div>

> ```
> <%= link_to_remote( "click here",
> :update => "time_div",
> :url => { :action => :say_when },
> :position => "bottom" ) %>
> 
> ```

When you say :position =\> “bottom”, that means that you want to stick  
the result of the render at the bottom of the div. remove the :position  
and you should be fine.  
The action that is called via link\_to\_remote is just the same as any old  
action, so you can render whatever rhtml you want, so if you want a  
textfield it’s no different to usual. You can also use rjs if you need  
to update more than one thing at once.

Fred
