• CORS woes on Heroku

    ,

    After spending the past 4 hours attempting to solve what boiled down to a rather simple problem, I figure I’d better blog about it to save someone else the time and effort.

    If you’ve been leveraging Passenger’s new –nginx-config-template command line option to add CORS headers to static assets served from a Rails app hosted on Heroku, and the CORS headers recently disappeared under mysterious circumstances… read on.

    I’ve been using the method described here to add CORS headers to custom fonts served from a Heroku-hosted Rails app that’s proxied by Nginx which handles serving static files. I recently updated to Rails 4.2.2 and suddenly, my custom fonts (.woff and .woff2 files) no longer had CORS headers on them.

    After the aforementioned hours spent scratching my head, I discovered that the latest version of the sprockets gem is generating asset digests that are 64 chars in length, where previously they had been 32. Nginx’s default regexp for identifying requests for static assets assumes the digest will be 32 chars long, like so:

    # Rails asset pipeline support.
    location ~ "^/assets/.+-[0-9a-f]{32}\..+" {
      error_page 490 = @static_asset;
      error_page 491 = @dynamic_request;
      recursive_error_pages on;</code>
    
      if (-f $request_filename) {
        return 490;
      }
      if (!-f $request_filename) {
        return 491;
      }
    }
    

    Changing the regexp to recognize digests that are 64 chars in length immediately solved the problem:

    location ~ "^/assets/.+-[0-9a-f]{64}\..+" {
       ...
    }
    

    I had to laugh after something so stupid and silly cost me a good chunk of my Saturday to debug. But at least it’s working now. My statically served custom fonts have the correct CORS headers and Chrome and Firefox are happy again.


Need help?

I’m an independent software developer available for consulting, contract work, or training. Contact me if you’re interested.


  • Generating database migrations with Merb

    I’ve been futzing with the goodness that is Merb lately. It’s nifty. Really nifty. The documentation is pretty rough though. I’ll be posting some of my discoveries here. Maybe it will help someone who, like me, was a tad lost early on.

    One of the first things that confused me was migrations. Merb has script/generate just like Rails, but script/generate --help didn’t list a generator for migrations. When I attempted to generate a migration, I was promptly greeted with this pleasantness:

    $ script/generate migration CreateUsers
    Started merb_init.rb ...
    Loading Application...
    Compiling routes..
    Loaded DEVELOPMENT Environment...
    Couldn't find 'migration' generator
    

    Turns out that Merb doesn’t support databases by default. The Merb-specific ActiveRecord plugin has to be installed and switched on before migrations become available.

    (There are several ORM plugins to choose from as documented in the README, but I’ve had very little success getting the others to work so far.)

    Generate your Merb project:

    sudo gem install merb
    merb my_project
    cd my_project
    

    Install the ActiveRecord plugin thusly:

    sudo gem install merb_activerecord
    

    Now open up config/dependencies.rb and uncomment the following line:

    use_orm :activerecord
    

    The first time you run script/generate after doing this, you’ll get:

    Started merb_init.rb ...
    No database.yml file found in my_project/config.
    A sample file was created called database.sample.yml for you to copy and edit.
    

    Copy config/database.sample.yml to config/database.yml and customize as needed. (Don’t forget to create the test and development databases.)

    Now generate your first migration:

    script/generate migration CreateUsers
    

    And you’re off to the races.

  • Who’s On First?

    Here’s a video to celebrate the new year. This is an imitation of the classic Abbott and Costello routine. My brother and I have been doing it for over a decade now. Here’s a version from 4 years ago at the NCHE conference in North Carolina. Enjoy!

  • MultiRails grows one release older

    MultiRails is a plugin that makes it easy to test Rails applications against multiple versions of Rails. Built by the cool guys at this place (mainly Rob Sanheim), MultiRails version 0.0.3 has just been released. Check it out.

  • UI for acts_as_tree

    The current project I’m on requires us to give administrators the ability to modify a tree of models built using acts_as_tree (now a plugin as of Rails 2.0). The acts_as_tree plugin is simple and elegant, but I’ve struggled to find a pre-built UI framework for acts_as_tree that is equally compelling. Can anyone recommend a good, simple (AJAX optional) plugin for generating a UI for acts_as_tree? Please post a comment.