shrine

File Attachment toolkit for Ruby applications

Github星跟踪图

Shrine

Shrine is a toolkit for handling file attachments in Ruby applications. Some highlights:

If you're curious how it compares to other file attachment libraries, see the
Advantages of Shrine. Otherwise, follow along with the Getting Started
guide
.

Add the gem to your Gemfile:

# Gemfile
gem "shrine", "~> 3.0"

Then add config/initializers/shrine.rb which sets up the storage and loads
ORM integration:

require "shrine"
require "shrine/storage/file_system"

Shrine.storages = {
  cache: Shrine::Storage::FileSystem.new("public", prefix: "uploads/cache"), # temporary
  store: Shrine::Storage::FileSystem.new("public", prefix: "uploads"),       # permanent
}

Shrine.plugin :activerecord           # loads Active Record integration
Shrine.plugin :cached_attachment_data # enables retaining cached file across form redisplays
Shrine.plugin :restore_cached_data    # extracts metadata for assigned cached files

Next, add the <name>_data column to the table you want to attach files to. For
an "image" attachment on a photos table this would be an image_data column:

$ rails generate migration add_image_data_to_photos image_data:text

Now create an uploader class (which you can put in app/uploaders) and
register the attachment on your model:

class ImageUploader < Shrine
  # plugins and uploading logic
end
class Photo < ActiveRecord::Base
  include ImageUploader::Attachment(:image) # adds an `image` virtual attribute
end

In our views let's now add form fields for our attachment attribute that will
allow users to upload files:

<%= form_for @photo do, f, %>
  <%= f.hidden_field :image, value: @photo.cached_image_data %>
  <%= f.file_field :image %>
  <%= f.submit %>
<% end %>

When the form is submitted, in your controller you can assign the file from
request params to the attachment attribute on the model:

class PhotosController < ApplicationController
  def create
    Photo.create(photo_params) # attaches the uploaded file
    # ...
  end

  private

  def photo_params
    params.require(:photo).permit(:image)
  end
end

Once a file is uploaded and attached to the record, you can retrieve the file
URL and display it on the page:

<%= image_tag @photo.image_url %>

See the Getting Started guide for further documentation.

Inspiration

Shrine was heavily inspired by Refile and Roda. From Refile it borrows the
idea of "backends" (here named "storages"), attachment interface, and direct
uploads. From Roda it borrows the implementation of an extensible plugin
system.

Similar libraries

  • Paperclip
  • CarrierWave
  • Dragonfly
  • Refile
  • Active Storage

Code of Conduct

Everyone interacting in the Shrine project’s codebases, issue trackers, and
mailing lists is expected to follow the Shrine code of conduct.

License

The gem is available as open source under the terms of the MIT License.

主要指标

概览
名称与所有者shrinerb/shrine
主编程语言Ruby
编程语言Ruby (语言数: 5)
平台
许可证MIT License
所有者活动
创建于2015-10-25 23:33:32
推送于2024-09-09 15:54:23
最后一次提交2024-08-22 17:32:40
发布数53
最新版本名称v3.6.0 (发布于 2024-04-29 10:05:49)
第一版名称v0.9.0 (发布于 2015-10-26 00:34:23)
用户参与
星数3.2k
关注者数38
派生数273
提交数2.4k
已启用问题?
问题数373
打开的问题数16
拉请求数210
打开的拉请求数7
关闭的拉请求数60
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?