link_to_remote udpates a div w/ javascript (Greybox) help

Hi all,

Once again I'm looking for all y'alls help. Currently I have a link_to_remote as follows:

<%=link_to_remote(product.name, :update => "selected_product", :url => {:action => "show", :id => product.code})%>

This link updates a div called selected_product. On the page that is rendered in the updated div, I use a Greybox plugin to show images, but when the div is updated, the Greybox is disabled for some reason. I found this site which seemed to fix my problem temporarily.

http://www.intridea.com/2007/11/21/link_to_remote-unobtrusively

It worked for some reason without having to re-do the link_to_remote helper, but after restarting my Webrick I get this error now:

try { } catch (e) { alert('RJS error:\n\n' + e.toString()); alert(''); throw e }

Here is my controller code:

  def show     @product = Product.find_by_code(params[:id])     if !@product       flash[:notice] = "Sorry, we couldn't find the product you were looking for"       redirect_to :action => 'index' and return false     end     @title = @product.name     @images = @product.images.find(:all)     @default_image = @images[0]     @variations = @product.variations.find(       :all,       :order => 'name ASC',       :conditions => 'quantity > 0'     )   respond_to do |format|     format.html{redirect_to :back}     format.js{render :update do |page|

    end }   end   end

The respond_to is a portion from the site above.

I tried the above link suggestion to edit the helper, but I get a error stating

super: no superclass method `link_to_remote'

So my question is using a link_to_remote, how can javascript be enabled in an updated div?

The page that you're showing alludes to the fact that it is an incomplete example. The comment that they've included in the render :update block (update the page dynamically) is not a statement indicating what happens as a result of executing the block -- it's a direction to you to do the work.

In the 'big picture', you're using two overlapping methods to update the div. Using the :update parameter on link_to_remote causes the framework to generate an Ajax.Updater call that expects an html response; the Updater will take the contents of the response and paste them into the dom element you specify. If you use this approach then your format.js actually needs to render the _html_ that the Ajax.Updater needs to update the 'selected_product' div.

The 'render :update' call on the server attempts to do something very similar. It assumes that you've made a request for javascript and provides a number of convenience methods for writing that javascript response. (It's the same thing as RJS, if you're read about that). If you use this approach you need to have your action use something like

page.replace_html 'selected_product', :partial=>'some-partial-that- renders-the-product-info'

Unfortunately, the two methods cannot be used together. (If you think about it, this makes sense... you're sending a request that expects html and sending a response in javascript). The javascript error that you're seeing -- the empty js try-catch -- is the default implementation of render(:update) with nothing added. If you're only updating one element on the page (as it sounds you are) then you're better off keeping the :update parameter and changing your format.js to render the html that it expects. If you need to update the page in more than one place then drop the :update parameter and write all the update in the render(:update) block. In the latter case you are actually better off writing an rjs file to promote better separation in your code (e.g., show.js.rjs). Rjs files need only contain the 'guts' of the js response; they are automatically wrapped in the render(:update) block for you.

HTH, AndyV

Andy, I truly appreciate your quick reply and help but well I'm still a noob so comprehending everything might be a little too much. I do want to stick with the :update parameter but don't quite understand what you meant by changing the "format.js to render the html". Could you please elaborate.

AndyV wrote:   If you're only