Alfa Jango Blog Engineering, Software, and Entrepreneurship

Posts Tagged ‘Ruby on Rails’

Remotipart 1.0 Released

Wednesday, August 31st, 2011

Just 6 weeks after the announcement of v0.4, I’m pleased to announce the release of Remotipart v1.0. With this release, AJAX file uploads in Rails 3.0 and 3.1 (building off the standard jquery-ujs driver) are as easy as humanly conceivable. This was possible thanks to a large push from Adam Kerr.

The two major changes for v1.0 are:

  • New jQuery 1.6 iframe-transport instead of dependency on form.js
  • New rack middleware does away with configuration

With the release of v1.0, the new officially maintained repo for Remotipart has moved from here to the JangoSteve fork on Github.

There is also now a more robust test suite. Read more about that in the Remotipart docs.

New jQuery iframe-transport

Previously, the process of submitting files via an ad-hoc iframe element and inserting the response back into the page (described here) was done by the form.js. For those who aren’t familiar, form.js is an awesome plugin that does a lot of really useful things. However, we weren’t really doing it justice by requiring it as a dependency for only one small part of its capabilities.

(more…)

Rails 3 AJAX File Uploads with Remotipart

Tuesday, July 12th, 2011

In my last article, we discussed the difficulties of file-uploads via AJAX, and how the iFrame method works around the issues to provide an AJAX-like interface for uploading files to the server.

So how does this relate to Rails?

Rails 3 uses Unobtrusive JavaScript for remote links and forms (and comes packaged with the jquery-ujs driver via the jquery-rails gem as of Rails 3.1!). However, because jquery-ujs relies on jQuery’s standard .ajax() function, it is incapable of doing AJAX file uploads.

The Remotipart gem

Remotipart to the rescue! The Remotipart gem does two things:

(more…)

New ajax:aborted Rails jQuery UJS Hooks

Tuesday, May 3rd, 2011

In my last article, Rails jQuery UJS: Now Interactive, I talked about my push to make Rails jQuery UJS (aka jquery-ujs) more interactive and easier to customize and extend with third-party plugins. Along those same goals, let’s discuss another recent change: ajax:aborted hooks.

There are now two scenarios where remote forms will not be submitted, and the AJAX submission will instead be aborted. When this happens, jquery-ujs now publishes hooks, to which we can bind handler functions to gracefully handle these situations.

Let’s take a look at each of these scenarios and how we can utilize the event hooks.

Scenario Hook name
blank required fields ajax:aborted:required
non-blank file inputs ajax:aborted:file

Required fields

(more…)

Rails jQuery UJS: Now Interactive

Friday, April 22nd, 2011

The Rails jQuery UJS adapter (aka jquery-ujs) has undergone some major renovations over the past few weeks. This particular change is one I’m very excited about, as I’ve been wanting to do this for a while. Without further ado, jquery-ujs is now InteractiveTM.

What do I mean by “interactive”? Well, we can now plug into every facet of jquery-ujs, binding to custom events, and even customizing internal functions, without hacking or monkey-patching the rails.js file itself.

We’ll start with a brief explanation of the change and then dive into a few examples.

Feel free to bypass my blabbering and check out the the commit.

Closure style

Previously, rails.js was one big closure.

Before:

(function($) {
  ...
  function fire(obj, name, data) {
    var event = $.Event(name);
    obj.trigger(event, data);
    return event.result !== false;
  }
  ...
})( jQuery );

// cannot access fire() function here

This closure design had many advantages. However, the biggest advantage, in this case, happened to be the biggest disadvantage as well: everything inside was self-contained and inaccessible by the outside world.

So, what if we like most of what jquery-ujs does for us, but just want to modify one little function? Before, we’d have to open rails.js in our editor and start hacking away. But no more!

Object literal style

(more…)

Rails js.erb Remote Response not Executing

Tuesday, March 15th, 2011

A common issue that comes up in the GitHub Issues for the Rails jQuery UJS and the Remotipart gem / jQuery plugin goes like this:

The JavaScript response from my js.erb is being interpreted as text and not being executed.

I usually point to this comment in GitHub Issues, which I posted the first time I came across this problem. But GitHub Issue comments are difficult to link to, and almost useless in search results due to all their ajaxy-ness. So, I’ll repost and explain here for future reference.

(more…)

Problems with Rails 3 Remote Links and Forms Using jQuery .live() in IE

Tuesday, November 16th, 2010

This will be a quick post. I just wanted to point out that there is a problem with AJAX response handling for Rails 3 remote links and forms in Internet Explorer. This problem affects applications still using jQuery 1.4.2.

Upon clicking a remote link or submitting a remote form, the following code will work in Firefox, Safari, and Chrome, but nothing will happen in Internet Explorer:

$('a, form').live('ajax:success', function(){
  alert('Success!');
});

The problem is that IE7 and IE8 do not “bubble” custom events, such as ajax:success, ajax:failure, etc., to the top of the DOM tree.

(more…)

method_missing:
A Rubyist’s Beautiful Mistress

Friday, October 8th, 2010

I’ve read a few articles lately (like this one), advocating the use for the method_missing method in Ruby.

Many people seem to have a passionate love affair with method_missing, but aren’t very careful in how they handle their relationship. So, I’d like to address the question:

How should I use method_missing?

If you don’t want to read through the reasons you should stay with your companion, define_method, and are just looking to rationalize your love-affair with method_missing, you can skip directly to when to use method_missing.

When to resist method_missing

First of all, never give in to your love affair with method_missing without taking a moment to realize how good you have it. You see, in your day-to-day life, you rarely need method_missing as bad as you think you do.

Day-to-day life: proxy methods

Case: I need to allow one class to use the methods of another class.

This is the most common use-case I’ve seen for method_missing. It’s especially popular in gems and Rails plugins. The pattern goes something like this:

class A
  def hi
    puts "Hi from #{self.class}"
  end
end

class B
  def initialize
    @b = A.new
  end

  def method_missing(method_name, *args, &block)
    @b.send(method_name, *args, &block)
  end
end

A.new.hi #=> Hi from A
B.new.hi #=> Hi from A

(more…)

Caching, Zipping, and (Amazon CloudFront) CDN For A Rails App

Friday, June 18th, 2010

This is Article #4 of a 4-part series. For a good primer, check out the first two articles listed below. For the reasoning and analysis behind the “Recommended” option in this article, check out Part 3, How to Combine GZip + CDN for Fastest Page Loads. Otherwise, jump right in!

  • The Importance of Page Load Speed
  • Improve Page Load Speed (by 80%) by Improving Component Load Speed
  • How to Combine GZip + CDN for Fastest Page Loads
  • Caching, Zipping, and (Amazon CloudFront) CDN For A Rails App
    • Prerequisites
      1. Cached stylesheets and javascripts
      2. Creating an Amazon AWS Account
    • Setup S3 Buckets
    • Setup CloudFront Distributions
    • Create CNAME records (optional)
    • Install Rails S3 Synch Plugin
      1. Installing AWS-S3 Gem
      2. Configure S3 Synch Plugin
      3. Add S3 Synch to Deployment
    • Option A: Compressible Assets from App Server, Images from CloudFront (recommended)
      1. Configure Rails Asset Host
      2. Create A-name Record
      3. Configure Apache
    • Option B: Serve Everything from CloudFront (easier, but not recommended)
      1. Configure Rails Asset Host
      2. Pre-compile Cached Stylesheet and Javascript File
    • Conclusion

In this article, we’re going to speed up our Rails application by up to 75%, simply by optimizing our Rails asset host. We’re going to serve our components (stylesheets, javascripts, images, etc.) from a combination of our app’s server and Amazon CloudFront (Option A, recommended), or entirely from CloudFront (Option B – easier).

The best option for you may depend on your specific needs, but I’ll cover both processes below. For a an in-depth analysis of why Option A is recommended over Option B, see the last article in this series, How to Combine GZip + CDN for Fastest Page Loads.

(more…)

Rails, Prototype, and JQuery in Harmony (or how to replace Prototype with JQuery)

Thursday, April 8th, 2010

It has become common practice to use JQuery as the javascript library of choice for Ruby on Rails applications. If you have a Rails app already using Prototype, it usually isn’t feasible to replace it in one swoop. And sometimes, you just simply need both to work at the same time. Whether you want to use both in unison (not recommended as a long-term strategy), or you want to gradually migrate from Prototype to JQuery, it’s not as scary as you may think.

The secret is that it is difficult to get Prototype to play nicely with JQuery, while it is rather trivial to get JQuery to play nicely with Prototype. This may seem like a minor distinction, but it is one that will save you a lot of headaches.

Use Prototype/Scriptaculous with JQuery

First, in our application layout, we’re going to get rid of <%=javascript_include_tag :defaults %> and include each javascript file explicitly just to make things a little less magicky and a little more straight forward.

(more…)

Printable Format for Any Webpage
(and the “Meat” Algorithm)

Tuesday, March 23rd, 2010

Last week, we added functionality to one of our web apps to show just the main content of any web-page, without all the other stuff. You may think of this as creating a printable view of any web-page, with all images, videos, ads, etc. removed. Here is an example of an original webpage vs. the printable view we create:

Feel free to skip straight to our “Meat” Algorithm, as we’ve so endearingly named it, if you’re not interested in the specifics of implementing it.

The Tools: Ruby and Nokogiri

Thanks to Ruby and a Ruby gem, called Nokogiri, it’s far easier to create this printable view than you may think. If you haven’t heard of it before, Nokogiri is a gem that reads and parses HTML, XML, and SAX, and allows you to easily search and manipulate these documents based on CSS selectors and XPATH.

(more…)