I’ve been messing with Padrino a bit over the past couple of weeks. There are several smallish apps that I’ve thought that Rails was a little bit of overkill for. However, what I didn’t realize that I was missing was the Asset Pipeline. It’s terribly useful, especially if you tend to mix technologies together in the same app. Once you get into the habit of using it, it gets hard to go without.

I decided to see if I could implement Sprockets directly into Padrino. It turned out to be fairly easy to do, especially since there was already a padrino-sprockets gem out there. Once I figured out the proper way to put everything together, it worked perfectly.

Here is a very quick walkthrough of how to set it up with a brand new Padrino app. Let’s create a basic Padrino application. I’ve basically picked my usual defaults, but it doesn’t matter too much which you use. The bonus is that any of the rendering and templates gems will be available for Sprockets already.

Add the following to your Gemfile. This will add the padrino-sprockets gem as well as the handlers for Coffeescript. That part’s optional, but given the Coffeescript love in Rails 3.1 and above, you’ll probably want it.

Run bundler and make sure all the gems are installed.

Now let’s move all of the stuff in public to app/assets instead. The default stylesheets and javascripts are fine defaults.

Replace app/assets/javascripts/application.js with the following. This is basically the default rails application.js file, and just automatically includes all other files in this directory. Putting the jquery and jquery-ujs files isn’t necessary, but putting them explicitly in the file makes sure that they show up before everything else.

And do the same for app/assets/stylesheets/application.css

The last thing we need to do is put the sprockets init code into app/app.rb config block. This just includes sprockets and padrino-sprockets and starts it up.

Now if you startup the server, you should be able to hit http://site/assets/application.js and http://site/assets/application.css, you should get the proper results.

The only issue I’ve found is that there is no easy way to override the directories for where stylesheets and javascripts are stored. This basically makes stylesheet_link_tag and javascript_include_tag useless. Instead, you’ll have to hardcode the following into the head section of your application layout.

Ted Kulp

Ted Kulp is a professional developer, author and technologist with over 15 years of experience from the suburbs of Philadelphia. In 2004, he created the popular content management system, CMS Made Simple and continued to run the project for over 8 years. These days, he makes his living from writing Javascript full-time, writing books, and hacking on open source.

More Posts - Twitter - Facebook - LinkedIn - GitHub

Notice: mc4wp_form is deprecated since version Mailchimp for WordPress v3.0! Use mc4wp_show_form instead. in /var/www/html/wp-includes/functions.php on line 4440

Find this interesting?

Sign up for my newsletter for more interesting content.