· 1 min read

Screencast: Datei-Upload mit CarrierWave

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] <%= image_tag painting.image_url(:thumb) if painting.image? %>

<%= form\_for @painting, :html => {:multipart => true} do |f| %> <%= f.error\_messages %> <%= f.hidden\_field :gallery\_id %>

<%= f.label :name %>
<%= f.text\_field :name %>

<%= f.file\_field :image %>

<%= f.label :remote\_image\_url, "or image URL" %>
<%= f.text\_field :remote\_image\_url %>

<%= f.submit %>

<% end %> \[/html\]
Back to Blog