CarrierWave on Heroku

As the new hotness in file attachments, CarrierWave (http://github.com/jnicklas/carrierwave) provides a really slick and easy way to attach files to your models.  It presents an issue if you run on Heroku, however, since CarrierWave stores temporary files in public/uploads/tmp by default.  It uses these temp files when the form is redisplayed so the uploads are not lost.  It is a cool feature, but a hassle if you are on Heroku since those folders are not writable.

Heroku does provide a place for you to keep temporary files though.  You can configure CarrierWave to use a custom cache dir by adding to your uploader:

def cache_dir
  "#{RAILS_ROOT}/tmp/uploads"
end

 

CarrierWave will then use one of the temp folders provided by Heroku and you will be able to use the temporary files in future requests.  We still have the problem of being able to display the images when the form is redisplayed though.  It is a pretty awesome feature that we don't want to miss out on and luckily we don't have to.

To be able to serve up the temporary images from the tmp folder, you need to make an additional action on one of your controllers that will take the cache location as a parameter and return the image.  

Put this in the resource's controller to respond with the image:

def image_cache
  headers['Cache-Control'] = 'public; max-age=600' # cache image for 10 minutes
  send_file "#{RAILS_ROOT}/tmp/uploads/#{params['cache_id']}/#{params['filename']}", :disposition => 'inline', :type => "image/png"
end

Put this in your routes file (where resource is the controller your new action is in):

map.resource_image_cache '/resources/image_cache', :controller => 'resources', :action => 'image_cache', :requirements => { :cache_id => /\d{8}-\d{4}-\d{5}-\d{4}/, :filename => /[a-zA-Z0-9_ ]+\.(jpg|jpeg|png|gif){1}/i }

And reference it like this in your view (where avatar is the field you mounted the uploader on):

= image_tag(resource_image_cache_path(:cache_id => resource.avatar_cache[/^[\d-]+/], :filename => resource.avatar_cache[/[a-zA-Z0-9 _\.]+$/])) if resource.avatar_cache

Special thanks to fellow Rocketeer Shay ( https://twitter.com/shayarnett ) for his input on this post.

jQuery SpicySelect 1.1.1

One more small update to the script this evening. The version 1.1 series includes the feature set providing keyboard support to the select. The minor revision deployed tonight added the ability to start typing the name of an option to select it. It performs a case insensitive search on the options in the list and matches to the first one that contains the string the user typed. Pressing backspace or delete will cause it to clear out the cache so you can type a different selection. I know that different browsers work slightly differently in this regard, but this is similar to how Safari works and seemed sensible to me.

Also to note is that the source code in the repo is always the latest and greatest revision, but I package release milestones for quick download from the Downloads tab on the github project page. You can get either the raw or minified version there.

http://github.com/paulelliott/jquery-spicyselect

jQuery SpicySelect v1.1

I just pushed some changes to spicyselect up to github.  This includes some really cool fixes and enhancements to the original script.

  • The mask is now in the tab order
  • Upon receiving focus, it will have a "focus" class added so you can style accordingly
  • While focused, pressing the down arrow will expand the options
  • With the options expanded, the up and down arrows will move the selected option up and down through the list
  • When selecting with the keyboard, a class of "current" will be added to the current selection for easy styling
  • With the options expanded, pressing enter or space will choose the selected option
Now the mask will act much more similarly to how an actual select box acts.  Next up is to wire up the rest of the keys to select options by typing a name.

I also updated the demo a bit.  It now has some basic styles and I provided a quick link to download the stylesheet as an example.

As always, ping me with any bugs, requests, or feedback!

Same Domain, New Platform

I have been hosting my own Wordpress blog for a while now and have gotten a little tired of having to manage it. It offers great flexibility, but also requires me to keep up with updates of the software and the server it runs on. It really isn't something I want to do, given that there are some really good hosted solutions out there.

As a whole I am getting away from wanting to host run my own VPS and getting into managed hosts instead. Good solutions exist for the things I need to do and letting someone else who is an expert on system administration deal with that gives me more time to do the things that are important to me, namely writing code.

So, here I am running on Posterous. So far it has been a good experience. The migration from Wordpress was very simple and making posts via email is actually pretty handy. It is also a Rails application, which of course holds a special place in my heart.

If you were subscribing to me via RSS, then be advised that the feed path has changed.

Concise Syntax for Partials

Typically to render a partial in rails you would type:

render :partial => 'my_partial', :locals => {:some => @thing, :else => @goes_here} 

I recently found that you can now simply type it like this:

render 'my_partial', { :some => @thing, :else => @goes_here }

It isn't in the documentation as far as I can tell, but works like a champ and is nice and concise.