Entries Tagged 'Rails' ↓

Rails Ajax for non javascript users

I’ve been adding Ajax to my site lately, and decided to see how my site worked without javascript.  I figure there are still some people somewhere who decide not to use (or don’t have access too, shutter the thought) javascript.  Dang, my site was broken.  Keep in mind that most of my javascript stuff is convenience only, but I was missing a major step to make sure that things worked even though javascript didn’t.

How did I make it work:

Here’s my first code:

<%= link_to_remote "#{image_tag "less.jpg", :border => 0} #{anchor.name}"
,                        :url => { :action => "hide_whole_anchor", :controller => "memory", :id => anchor }
,                        :loading => "Element.show('spinner')"
,                        :complete => "Element.hide('spinner')"

Doesn’t look so bad right?  Here’s the problem, the action of the form is just #, meaning when you click on it come right back to this page.  This doesn’t work for me :(.  Instead I had to add:

,                        :html => {:href => url_for(:action => "view_all", :controller => "memory", :id => anchor.id)}

after the :url line.  Then I setup view_all to listen for an id and open or close an anchor based on the anchor id (don’t worry if you have no idea what an anchor is).

This line should be found in each and every link_to_remote and form_remote_tag etc.  This way when all those non javascripters come to your page, things still work.

Ruby on Rails - Ajax not working on production, but is on dev

So I decided that I had to write on this, because it was sooooo annoying.

I’m currently writing a rails website (www.memorysketch.com).  I’ve been adding Ajax to it to make it cool.  Everyone loves ajax right?  The problem is that Ajax problems are harder to diagnose.  Nothing happens, and nothing tells you why.

The problem is that on my development box everything worked perfectly, but on my production box the ajax stuff just didn’t work.  No error, or anything, it just didn’t update the div properly.  Ends up that I forgot to delete the rhtml file :(. I had a create.rjs file that updated the div, and a create.rhtml file still sitting there on the production server but not on the dev box.  Very annoying.

So, if ajax just isn’t working, and you’re not getting any errors, make sure that you don’t have an rhtml file with the same action name.