Hiding and showing html with ajax


#1

I could use some advice on this situation. I know this looks like a
lot of code, but It is a straightforward implementation question, I am
not stuck or anything. I have what I need working but it involved two
ajax server requests and Im not sure it is the best way. I have a
list of appointments, each with dates and an edit link. This edit
link is a link_to_remote function that makes a call to the server and
renders a partial that replaces the div that contains the appointment
information with editable fields:

<%= appointment.end_time.strftime("%l:%M%p").sub(/^0/,'') %> <%= link_to_remote "Edit", :url => {:action => "edit_appt", :id => appointment.id, :day => day} %>

I want to make this a ajax request, b/c this is all in an ajaxed popup
and I would like to speed them up by not loading all the editable
field html for every appointment for each popup. Is this a safe
assumption and solution?

The edit_appt action looks like this:

page.replace_html “appt_#{params[:day]}_#{params[:id]}”, :partial =>
‘edit_appt’, :object => @appt_to_edit, :locals => {:day => params
[:day]}

Now if anyone is still with me(and I REALLY appreciate if you are!)
then my question is about placing a cancel link/button in this
edit_appt partial that will load the original appointment. The way it
is set up now is that it makes another ajax request, passes the id
again, performs a search and then renders another partial:

def reload_appt
@appointment = Appointment.find(params[:id])
respond_to do |format|
format.js do
render :update do |page|
page.replace_html “appt_#{params[:day]}_#{params
[:id]}”, :partial => ‘individual_appt’, :object => @appointment,
:locals => {:day => params[:day]}
end
end
end
end

My question is if this is the best way of doing it? Im wondering if
there is some way to use hide/show html or something, b/c the second
database query seems redundant, seeing as how the appointment html
existed in the first place. Thanks for any advice.


#2

Hi David,

David wrote:

My question is if this is the best way of doing it?

Depends on your definition of ‘best’. It works and it’s easy to follow
the
code. Good enough for me.

Im wondering if there is some way to use hide/show html or something,

Sure, but you’d have to rework your view so you’d have a separate set of

s for the originals which you could hide/show, and a second set into which you'd render the edit partials.

b/c the second database query seems redundant, seeing as how the
appointment html existed in the first place. Thanks for any advice.

DRY is not an absolute. Neither is readability. Both are components of
maintainability. Go for balance. IMHO, if there’s a tie, readability
wins.
YMMV.

HTH,
Bill


#3

Okay, thanks for the input.