small medium large xlarge

Generic-user-small
03 Dec 2008, 16:01
James West (104 posts)

Hi

      respond_to do |format|
        format.js if request.xhr?
        format.html {redirect_to_index}
      end

Doesn’t work properly

I have been merrily playing around with AJAX for a couple of weeks now and very successfully or so I thought until I tried to cater for non xhr enabled browsers. I had the following

  def add_to_cart
    begin
      product = Product.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error("Attempt to access invalid product #{params[:id]}")
      redirect_to_index("Invalid product")
    else
      @cart = find_cart
      @current_item = @cart.add_product(product)
      respond_to do |format|
        format.js
      end
    end
  end

and everything was really cool. I was hitting the depot app pretty heavily and it just worked. I hasten to add that I’m using firefox 3 with a webrick server on windows xp sp3 I then decided to have a look at the site using i.e. 6 and nothing worked.

This was kinda cool because I had security settings set to very high and I wanted to see what happened if AJAX was not enabled.

So I turn to page 141 of the latest (B1.6) version of the pdf and take a long hard look at how to degrade nicely.

I consequently changed my code to

  def add_to_cart
    begin
      product = Product.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error("Attempt to access invalid product #{params[:id]}")
      redirect_to_index("Invalid product")
    else
      @cart = find_cart
      @current_item = @cart.add_product(product)
      respond_to do |format|
        format.js if request.xhr?
        format.html {redirect_to_index}
      end
    end
  end

as per book adding in the xhr? check and the format.html {redirect_to_index} bit.

I then restarted my server just to be on the safe side and tested in i.e.6 again

TADA :-) BRILLIANT what didn’t wirk before now works beautifully.

Now I spend a day playing around in i.e. and all is good. So this morning I load up my reliable, all singing and all dancing Firefix 3 to do some surfing on the net and out of curiosity I took a look at the site I’d uploaded to the net the night before and nothing is working.

In a panic I load up my development environment and test that too. NOPE! Some requests were running like a dog, and I mean over 60 seconds to display the cart and an ajax request I had been playing with to empty the cart didn’t work at all.

I switched back to i.e. and everything was great. I reverted the code back to my first example and everything was superb in Firefox and nothing was working in i.e..

I have been through all sorts of combinations now both on my p.c. and on the net and my conclusion is that this check for xhr? just doesn’t work properly.

So. Is there a proper way to degrade from AJAX and still have AJAX working when it is enabled in a browser?

Is the if statement falling through and running both commands rather than just the one. i.e. should there be an else clause?

or is this a case of Rails 2.1.2 needing to be coded for either 1 or the other?

Sam. If you are reading this I can heartily recommend you try it out as I have described and I would love to know what conclusions you can come up with and do you want this reporting as errata?

I really don’t have the Rails nounce yet to work this out although thanks to this book and the rails forum community I’m getting there :-)

An afterthought -

I’m going to try re-structuring the condition to something like

      respond_to do |format|
        if request.xhr?
          format.js
        else
          format.html {redirect_to_index}
      end

I have no idea if this will work

Cheers

James

You must be logged in to comment