Screencast: Datei-Upload mit CarrierWave

CarrierWave ist ein Gem, dass für Dateiübertragungen in Ruby eingesetzt werden kann. Es vereinfacht nicht nur die Uploads, sondern unterstützt dabei auch eine verschiedenste ORMs, Frameworks und Bildverarbeitungstools wie z.B. image-magick.

 

Download:

Download(20.7 MB, 9:05)
Alternativer Download für iPod & Apple TV(18.5 MB, 9:05)

 

Resourcen:

 

Quellcode:

[bash]
bundle
rails g uploader image
rails g migration add_image_to_paintings image:string
rake db:migrate
[/bash]

[ruby]
# Gemfile
gem "rmagick"
gem "carrierwave"

# models/painting.rb
class Painting < ActiveRecord::Base
attr_accessible :gallery_id, :name, :image, :remote_image_url
belongs_to :gallery
mount_uploader :image, ImageUploader
end

# app/uploaders/image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include CarrierWave::RMagick

storage :file

def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end

version :thumb do
process :resize_to_limit => [200, 200]
end
end
[/ruby]

[html]
<!– galleries/show.html.erb –>
<%= image_tag painting.image_url(:thumb) if painting.image? %>

<!– paintings/_form.html.erb –>
<%= form_for @painting, :html => {:multipart => true} do |f| %>
<%= f.error_messages %>
<%= f.hidden_field :gallery_id %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<p>
<%= f.file_field :image %>
</p>
<p>
<%= f.label :remote_image_url, "or image URL" %><br />
<%= f.text_field :remote_image_url %>
</p>
<p><%= f.submit %></p>
<% end %>
[/html]