Skip to content

Instantly share code, notes, and snippets.

@assimovt
Created April 3, 2011 08:27
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save assimovt/900289 to your computer and use it in GitHub Desktop.
Save assimovt/900289 to your computer and use it in GitHub Desktop.
Carrierwave uploader sample
# app/views/photos/_photo.html.haml
.photo[photo]
= image_tag photo.image_url(:thumb) if photo.image?
= link_to "Delete", property_photo_path(@property, photo), :method => :delete, :confirm => "Are you sure?", :remote => :true
<%- # app/views/photos/destroy.js.erb -%>
<%- if flash[:error].blank? -%>
$('#photo_<%= @photo.id %>').remove();
<%- end -%>
# app/views/properties/edit.html.haml
- key = Rails.application.config.session_options[:key]
# Needs yield :scripts in application layout in the bottom of the body
= content_for :scripts do
:javascript
var upload_params = {
'#{key}' : '#{cookies[key]}',
'#{request_forgery_protection_token}' : '#{form_authenticity_token}',
'_http_accept': 'application/javascript'
};
var url = $('input#photo_image').attr('rel');
$('input#photo_image').uploadify({
'uploader' : '/assets/uploadify.swf',
'script' : url,
'fileDataName' : 'photo[image]',
'fileExt' : '*.png;*.jpg;*.gif',
'cancelImg' : '/images/cancel.png',
'multi' : true,
'scriptData' : upload_params,
'auto' : true,
'onComplete' : function(e, id, obj, response, data) {
$('#photos').append(response);
}
});
#photos
= render :partial => 'photos/photo', :collection => @property.photos
- unless @property.new_record?
= fields_for Photo.new do |f|
= f.file_field :image, :rel => property_photos_path(@property)
# app/uploaders/photo_uploader.rb
class PhotoUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
# Include RMagick or ImageScience support:
# include CarrierWave::RMagick
# include CarrierWave::ImageScience
# Choose what kind of storage to use for this uploader:
storage :file
# storage :s3
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
# Provide a default URL as a default if there hasn't been a file uploaded:
# def default_url
# "/images/fallback/" + [version_name, "default.png"].compact.join('_')
# end
# Process files as they are uploaded:
# process :scale => [200, 300]
#
# def scale(width, height)
# # do something
# end
version :thumb do
process :resize_to_fit => [108, 81]
end
# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
def extension_white_list
%w(jpg jpeg gif png)
end
# Override the filename of the uploaded files:
# def filename
# "something.jpg" if original_filename
# end
end
# app/controllers/photos_controller.rb
class PhotosController < ApplicationController
before_filter :require_user
before_filter :find_property
before_filter :find_or_build_photo
def create
respond_to do |format|
unless @photo.save
flash[:error] = 'Photo could not be uploaded'
end
format.js do
render :text => render_to_string(:partial => 'photos/photo', :locals => {:photo => @photo})
end
end
end
def destroy
respond_to do |format|
unless @photo.destroy
flash[:error] = 'Photo could not be deleted'
end
format.js
end
end
private
def find_property
@property = current_user.properties.find(params[:property_id])
raise ActiveRecord::RecordNotFound unless @property
end
def find_or_build_photo
@photo = params[:id] ? @property.photos.find(params[:id]) : @property.photos.build(params[:photo])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment