March, 2009


14
Mar 09

Dynamically set a domain for a Rails asset host

I’ve been wanting to implement an asset host for my Rails app, looking over the API I saw that Rails’ asset_host only supported a single domain. I’m running multiple domains off one Rails app, so this would have been a problem. After a little research, I came up with a solution that would work for me.

I run this as a before_filter in my Application Controller.

# for my use, @site.domain returns the user's current
# domain. you should change this to whatever method
# you use to get the domain

class ApplicationController < ActionController::Base
before_filter :find_asset_host
 
private

  def find_asset_host
    ActionController::Base.asset_host = Proc.new { |source|
        asset_hosts = %w{icarus zeus aphrodite etc etc}
     
        # disable asset host for development
        if is_development?
          ""
        else
          "http://#{asset_hosts[rand(4)]}.#{@site.domain}"
        end
      }    
  end
 
end

6
Mar 09

Simple non-model check box properties

Need to pass a property that isn’t associated with a model? I did. It took me a few minutes, but the solution is codetty easy.

Instead of passing the instance variable your form is using, try this.

<% form_for [:admin, @post]} do |f| %>
  <%= f.error_messages %>
 
  <p>
    <%= f.label :title %>
    <%= f.text_field :title %>
  </p>

  <p>
    <%= label(:skip_queue, "Skip queue") %>
    <%= check_box("overrides", "skip_queue") %>
  </p>

  <p>
    <%= f.submit "Submit" %>
  </p>
<% end %>

You’d access the property of the check box like so:

# log
Processing Admin::PostsController#create (for 127.0.0.1 at 2009-03-06 03:03:01) [POST]
  Parameters: {"commit"=>"Update", "post"=>{"title"=>"Title", "overrides"=>{"skip_queue"=>"1"}}

# access the property
params[:overrides][:skip_queue]

5
Mar 09

Paperclip deletes images with empty file fields update with rails 2.3

Bahhh! When you update a model that contains an empty file field in Rails 2.3 the attachment is deleted. It looks like empty file fields get passed as nil and then Paperclip gets angry.

Processing Admin::PostsController#update (for 127.0.0.1 at 2009-03-05 01:21:08) [PUT]
  Parameters: {"commit"=>"Update", "post"=>{"title"=>"Post Title", "image"=>nil}, "id"=>"124"}

There are two quick monkey patches that I’ve been using, the first, was splitting off attachments to their own method (edit_attachment)…but this quickly became a nuisance. A quicker work around is to remove the attachment field, in this case “image”, from the parameters.

# PUT /posts/1
# PUT /posts/1.xml
def update
  @post = Post.find(params[:id])

  # delete the offending parameter
  params[:post].delete(:image) if params[:post][:image].nil?

  respond_to do |format|
    if @post.update_attributes(params[:post])
      flash[:notice] = 'Post was successfully updated.'
      format.html { redirect_to(admin_post_url(@post)) }
    else
      format.html { render :action => "edit" }
    end
  end
end

3
Mar 09

Watermark your images in Rails

I’ve been working on a project that requires me to watermark all my images. Here’s a script I put together using rmagick. I plan on releasing a skeleton application that will accept uploaded files and watermark them.

Ideally, I’d like to use paperclip to handle uploading, and write a processor subclass to handle watermarking…unfortunately I’m running into some issues. If anybody wants to lend a hand, I have the public repository up at: http://github.com/ng/paperclip-watermarking-app/tree

require 'RMagick'

# load source and watermark images
src = Magick::ImageList.new("source-image.jpg").first
watermark = Magick::Image.read("watermark-image.png").first

# create a new, blank image with a height that is the sum of
# the source and watermark
dst = Image.new(src.columns, src.rows+watermark.rows) { self.background_color = 'white' }

# place the source image on the blank image
result = dst.composite(src, Magick::NorthGravity, Magick::OverCompositeOp)

# place the watermark over the codevious transformation
result = result.composite(watermark, Magick::SouthEastGravity, Magick::OverCompositeOp)

# write it out
result.write('watermarked-image.jpg')

2
Mar 09

Paperclip not saving your images?

I’ve had a few guys email me asking if I had any idea why Paperclip wasn’t saving their uploaded images. Here’s a form one of them sent me:

<% form_for(@post) do |f| %>
  <%= f.error_messages %>


    <%= f.label :title %>
    <%= f.text_field :title %>
 


    <%= f.label :description %>
    <%= f.text_area :description %>
 

  <%= f.file_field :image %>


    <%= f.submit "Update" %>
 

<% end %>

Notice anything different on this form?

<% form_for(@post, :html => { :multipart => true }) do |f| %>
  <%= f.error_messages %>


    <%= f.label :title %>
    <%= f.text_field :title %>
 


    <%= f.label :description %>
    <%= f.text_area :description %>
 

  <%= f.file_field :image %>


    <%= f.submit "Update" %>
 

<% end %>

Simple mistake, but I’ve done it a few times in the early AM and wondered WTF was going on!

:html => { :multipart => true