Making Ajax rerender in div

In Onlamp there is a wonderful introduction to Ajax under Rails in
“Ajax on Rails,” (
Radar – O’Reilly ).

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

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

  <%= 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