Using with Rails
Setup
Gemfile:
Command line:
rails generate dragonfly
This generates a file in config/initializers/dragonfly.rb
.
See Configuration for customizations.
Handling attachments
Supposing we have a Photo
model that we wish to add an image
attribute to.
Dragonfly provides the accessor by adding to the model
but it needs an image_uid
column to work, so you’ll need a migration with
or equivalent.
View for uploading:
Rails < 3.2: You’ll need to add :html => {:multipart => true}
to the form tag helper.
We need to allow the parameter image
to be accepted by the controller.
In Rails 4, you’ll need
or something similar in the controller, and in Rails 3
in the model.
View for displaying:
More with models…
There are many more things you can do with models, such as making thumbnails on upload, or persisting attachments even when validations fail.
See Models for more details.
Caching
The simplest way to cache content in Rails is with Rack::Cache.
In your Gemfile, for production environment you should have
Then add Rack::Cache to the middleware stack in production environment. See below for how to do that for your Rails version.
Rails 4
Uncomment the line in config/environments/production.rb
Rails 3.1 - 3.2
Nothing to do! Rack::Cache is already inserted for you in production.
Rails 2.3 - 3.0
in config/environments/production.rb
Custom endpoints
routes.rb
:
Text generation example
Endpoint callable from javascript. e.g. /image?file=egg.png&size=30x30
Derived from theme by orderedlist