File Uploads via Merb inside a Rails Application : Part 1 of 3
I’ve been working with Ruby on Rails for about 2 years now, sucking knowledge from the community via google searches, mailing lists and the killer devs I’ve had the fortune to work with. My desire to payback the community has always been there; I just never found myself bleeding profusely enough from the cutting edge to feel that my solutions were need. Unless you count the Rails plugin that I never actually published, mostly cause once you’ve done it, well, it just seems so easy/obvious. Anyway I seem to find myself in a position to give back, so without rambling on, I give you…
File Uploads via Merb inside a Rails Application : Part 1 of 3
Assumptions:
- You are running Rails (1.2.3 as of this writing)
- You are serving up a Mongrel Cluster (Mongrel 1.0.1) via Apache (2.0+)
- You are uploading files currently using Rick Olsen’s attachment_fu (this example happens to use Amazon’s S3 service)
- Merb version as of this writing is 0.3.7
- We all give some kudos to Ezra Zygmuntowicz!
Overview:
- Install Merb and get it running
- Let Merb get a piggyback ride from our Rails sessions
- Sharing is good, let’s share some Models
- Merb-ify attachment_fu
- Merb-Rails synergy
- Apache 2: Electric boogaloo (a.k.a mod_rewrite)
- Capistrano love
Now since the start of this is basically (steps 1 - 3.5) from Luke Franci’s post on RailSpikes, I will direct you there for Merb installation and to get an overall sense of how to work with Merb. Thanks Luke! When you come back we will then add your fancy new Merb app (I called mine MerbUploader) to your existing Rails app. For details on Merb go here http://merb.rubyforge.org/files/README.html.
(Black Sabbath’s “Fluff” playing in the background)
Oh… welcome back. Sorry, the place is a mess, didn’t expect you back so soon… err… Ok! So now what? First just make sure your Merb app is working.
For the lazy among us, here’s the gist:
(note: I added hpricot, not an official dependency but I needed it)
> gem install mongrel json erubis mime-types archive-tar-minitar rspec hpricot -y
> gem install merb -y
> merb -g merbuploader (you can call it whatever you want)
> cd merbuploader
> merb start
You should get something similar to this:
Merb started with these options:
---
:cache_templates: false
:environment: development
:use_mutex: true
:show_error: true
:session_id_cookie_only: true
:dist_root: T:/merbuploader/dist
:sql_session: true
:host: 127.0.0.1
:query_string_whitelist: []:allow_reloading: true
:port: "4000"
:log_level: debug
:merb_root: T:/merbuploaderYou need to install the mailfactory gem to use Merb::Mailer
you must install the markaby gem to use .mab templates
you must install the haml gem to use .haml templates
ActiveRecord session mixed in
Rails session compatibilty on.
Compiling routes..
merb init called
Connecting to database...
Loaded DEVELOPMENT Environment...
Once we see that Loaded DEVELOPMENT Environment all is good. You can close that down now.<Crtl-C>
Sweet! Now we are going to move that newly created merbuploader dir to the root level of our Rails app. So you get something like:
app
config
db
doc
lib
merbuploader
public
…
Now that we did some fun stuff, let’s take care of something a little less exciting. We want to allow Merb to get in on the Rails session action, since they will share the same database this works out well for us. Here’s a Rails migration to move your sessions. Yes it may just be a migration, but the fact the Merb can piggyback on these sessions is pretty cool.
db/migrate/000_add_sessions.rb
class AddSessions < ActiveRecord::Migration
def self.up
create_table :sessions do |t|
t.column :session_id, :string
t.column :data, :text
t.column :updated_at, :datetime
end
add_index :sessions, :session_id
add_index :sessions, :updated_at
enddef self.down
drop_table :sessions
end
end
Now we need to tell Rails what we did, drop this line into your 3 environment files in your Rails application.
config/environments/*.rb
config.action_controller.session_store = :active_record_store
So run that migration, restart your app and… come back next week for part 2.
Now with Part 3!
Disclaimer: This may not be the only or even the best way to do things. Some of this is uglier that I would like, so if anyone has suggestions please post them! Thanks!
Related Articles
4 Comments so far
Leave a reply
I really can’t show enough gratitude for these articles. Simply brilliant… I love it!
[…] you want to start at the beginning check out Part 1 and Part […]
I implemented this in Merb awhile ago and found it to be a big pain. Now I had multiple apps to babysit, and I had to write a bunch of hacks to get my models working in Merb. It was easier to store the files on the server and create a queue to process them _outside_ of the rails action. This way if S3 is down (assuming you’re using attachment_fu with the s3 backend), you can restart the job later, rather than presenting the user with an error message that their upload failed.
Think there is a typo.
The line:
> cd merbuploader
> merb start ——–> this should be just merb I suppose else it creates a new merb app called start.
Now sure if I am right but just merb worked