Forum: Ruby on Rails hiding and showing html with ajax

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
David (Guest)
on 2009-01-19 18:56
(Received via mailing list)
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:

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

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.
Bill W. (Guest)
on 2009-01-19 23:14
(Received via mailing list)
Hi David,

David wrote:
<snip>
> 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
<div>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
David (Guest)
on 2009-01-20 05:48
(Received via mailing list)
Okay, thanks for the input.
This topic is locked and can not be replied to.