Brandon Harris | Active Scaffold with Paperclip

Active Scaffold with Paperclip

I always recommend Active Scaffold to fellow Rails developers. There is a learning curve, but it can cut a lot of development time out of your clients custom CMS.

Paperclip has pretty much dominated the Rails attachment market for the past year. My company has migrated to it, and there are countless tutorials and testimonials regarding it’s ease of use.

I am assuming that you are using the latest version of Rails (2.3.5 as of this writing), Active Scaffold, and Paperclip. First we will install all the necessary components, then we will configure the rails app.

Install Active Scaffold:

script/plugin install git://github.com/activescaffold/active_scaffold.git

Active Scaffold is big so this takes a while.

Install Paperclip:

script/plugin install git://github.com/thoughtbot/paperclip.git

As of 6/14/2010 this is no longer needed, the bridge has been moved to the core
Install the Active Scaffold Paperclip Bridge:

Lets create a basic person model:

script/generate model Person

To create controllers in the admin namespace, I don’t use the rails controller generator. I don’t like cluttering up my projects with files I will never use, since I employ Active Scaffold for 99% of my admin interfaces. This is my personal preference, you may do this anyway that you wish.

mkdir app/controllers/admin && touch app/controllers/admin/people_controller.rb

Lets configure the “Person” migration for paperclip.

 1 class CreatePeople < ActiveRecord::Migration
 2   def self.up
 3     create_table :people do |t|
 4       t.string :name, :avatar_file_name, :avatar_content_type
 5       t.integer :avatar_file_size
 6       t.datetime :avatar_updated_at
 7       t.timestamps
 8     end
 9   end
10 
11   def self.down
12     drop_table :people
13   end
14 end

Configure the Person model to accept the paperclip attachment.

1 class Person < ActiveRecord::Base
2   has_attached_file :avatar, :styles => {:thumbnail => "75x75#"}
3 end

Setup your routing for the admin namespaced “People” controller.

1 ActionController::Routing::Routes.draw do |map|
2   map.namespace :admin do |admin|
3     admin.resources :people, :active_scaffold => true
4   end
5   map.connect ':controller/:action/:id'
6   map.connect ':controller/:action/:id.:format'
7 end

Create a layout file for admin, I use Haml, and I recommend that you do too.

1 !!!STRICT
2 %html{'xmlns' => "http://www.w3.org/1999/xhtml", :lang => "en"}
3   %head
4     = javascript_include_tag :defaults
5     = active_scaffold_includes
6   %body
7     #content
8       = yield

Finally, configure your active scaffolded controller. The key here is to include the attachment model name, in this case “avatar”.

 1 class Admin::PeopleController < ApplicationController
 2   layout 'admin'
 3   active_scaffold :person do |config|
 4     #restrict all columns to these three
 5     config.columns = [:name, :avatar]
 6 
 7     #include multipart for create and update forms
 8     config.create.multipart = true
 9     config.update.multipart = true
10   end
11 end

When you are done you can go to “/admin/people” and you will be greeted with the familiar active scaffold interface. Create a new Person and upload the avatar to be used for that Person. Your result should be similar to this image:
as_paperclip_done.png

You should download the source for this from my git repo here.

Tags

  • e (1)