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.
padrino g project something -t cucumber -s jquery -e slim -c compass -e slim cd something
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.
# Use Sprockets gem 'padrino-sprockets', :require => 'padrino/sprockets' # Use Coffeescript gem 'coffee-script' # rack-coffee shouldn't be needed # gem 'rack-coffee', :require => 'rack/coffee'
Run bundler and make sure all the gems are installed.
mkdir -p app/assets mv public/* app/assets/ rm app/assets/favicon.ico
And do the same for app/assets/stylesheets/application.css
/* * This is a manifest file that'll automatically include all the stylesheets available in this directory * and any sub-directories. You're free to add application-wide styles to this file and they'll appear at * the top of the compiled file, but it's generally better to create a new file per style scope. *= require_self *= require_tree . */
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.
# Sprockets support require 'sprockets' register Padrino::Sprockets sprockets
Now if you startup the server, you should be able to hit
http://site/assets/application.css, you should get the proper results.