8Rays Tech Blog


Essential reading for the discerning Rails developer

No finger shall be spared during the typing of this blog.

I am always excited to learn a new programming language or framework. My focus in this post is how to expand the horizons for someone coming new to Ruby on Rails. It’s an incredibly powerful framework and has a huge community pushing it along.

continue reading...

Display a spinner on Ajax requests in jQuery and Ruby on Rails

Here’s how you can display a spinner on each Ajax request in Ruby on Rails 3 using jQuery and HTML5 “data-” attributes.

The setup part

//public/javascripts/application.js
//"ajax:beforeSend" and "ajax:complete" event hooks are provided by Rails 3's jquery-ujs driver.
$("*[data-spinner]").live('ajax:beforeSend', function(e){
  $($(this).data('spinner')).show();
  e.stopPropagation(); //Don't show spinner of parent elements.
});
$("*[data-spinner]").live('ajax:complete', function(){
  $($(this).data('spinner')).hide();
});
#app/helpers/application_helper.rb
def spinner_tag id
  #Assuming spinner image is called "spinner.gif"
  image_tag("spinner.gif", :id => id, :alt => "Loading....", :style => "display:none")
end

The effect part

Now in whichever form or link you’d like to add spinner support just add one “data-spinner” attribute and the spinner image.
Here’s an example -

continue reading...

Redirecting www URL requests to non-www URL (and vice-versa) in Rails 3

It is probably not a good idea to have your website reachable from both “www.” URL as well as “non-www” top-level URL. As this diverts traffic (and bookmarks) to two different URIs while also possibly splitting up search-engine’s page-ranking. While “www.” URLs have their own advantages (e.g., familiarity, cookie-less sibling subdomain), non-www URLs take at least one less DNS query to resolve and are shorter.

Here is how to achieve the redirection in Rails 3:

continue reading...

How to deprecate methods and constants in Rails 3

In programming terminology, the term “deprecation” is applied to software features that have been superseded and will be removed in future releases. Deprecated features usually generate warning on use and are only present for backward-compatibility, giving enough time to developers to migrate their code to new features.

continue reading...

Featured posts

Subscribe to this blog

Blog topics