Created
June 11, 2015 05:13
-
-
Save sandyxu/f783395e199d8a129da3 to your computer and use it in GitHub Desktop.
how to use carrierwave
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= form_for Attachment.new do |f| %> | |
<%= f.label :file, '上传文档', class: 'btn btn-primary btn-sm' %> | |
<%= f.file_field :file %> | |
<span class="text-danger">文件大小不能超过20M</span> | |
<%= f.submit 'save', class: 'btn btn-primary btn-sm' %> | |
<% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Attachment < ActiveRecord::Base | |
UPLOAD_LIMIT = 20.megabytes | |
mount_uploader :file, FileUploader , mount_on: :name | |
belongs_to :attachmentable, polymorphic: true | |
validates :file, presence: true | |
validate :file_size_validation, if: 'file?' | |
before_save do | |
self.size = self.file.size | |
self.content_type = self.file.content_type | |
end | |
def size_display | |
kb = size.to_f / 1.kilobyte | |
if kb / 1.kilobyte > 1 | |
mb = kb / 1.kilobyte | |
"#{ '%.2f' % mb }MB" | |
else | |
"#{ '%.2f' % kb }KB" | |
end | |
end | |
private | |
def file_size_validation | |
if file.size > UPLOAD_LIMIT | |
errors.add(:file, "不能超过#{UPLOAD_LIMIT / 1.megabytes}MB") | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BaseUploader < CarrierWave::Uploader::Base | |
storage :file | |
def sanitize_filename(filename) | |
filename.strip.tap do |name| | |
# NOTE: File.basename doesn't work right with Windows paths on Unix | |
# get only the filename, not the whole path | |
name.sub! /\A.*(\\|\/)/, '' | |
# Finally, replace all non alphanumeric, underscore | |
# or periods with underscore | |
name.gsub! /[^\w\.\-]/, '_' | |
end | |
end | |
def store_dir | |
# uploads/file/000/000/001/ | |
"uploads/#{mounted_as}/#{('%09d' % model.id).scan(/\d{3}/).join('/')}" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FileUploader < BaseUploader | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CreateAttachments < ActiveRecord::Migration | |
def change | |
create_table :attachments do |t| | |
t.integer :attachmentable_id | |
t.string :attachmentable_type | |
t.string :name | |
t.string :content_type | |
t.integer :size | |
t.integer :download_count, default: 0 | |
t.datetime :deleted_at | |
t.timestamps null: false | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment