Shrine

Shrine

  • Guides
  • Plugins
  • External
  • Discourse
  • GitHub
  • Wiki

›Form

Attachment

  • Active Record
  • Column
  • Entity
  • Model
  • Sequel

Flow

  • Backgrounding
  • Presign Endpoint
  • Upload Endpoint

Processing

  • Derivation Endpoint
  • Derivatives

Source

  • Data URI
  • Rack File
  • Remote URL

Validation

  • Remove Invalid
  • Validation
  • Validation Helpers

Metadata

  • Add Metadata
  • Determine MIME Type
  • Infer Extension
  • Metadata Attributes
  • Refresh Metadata
  • Restore Cached Data
  • Signature
  • Store Dimensions
  • Type Predicates

Downloading

  • Download Endpoint
  • Rack Response
  • Tempfile

Form

  • Cached Attachment Data
  • Form Assign
  • Remove Attachment

Settings

  • Default Storage
  • Default URL
  • Dynamic Storage
  • Multi Cache
  • Pretty Location
  • Upload Options
  • URL Options

Other

  • Atomic Helpers
  • Included
  • Instrumentation
  • Keep Files
  • Mirroring
Edit

Form Assign

The form_assign plugin allows attaching file from form params without a form object.

plugin :form_assign

The Attacher#form_assign method will detect the file param and assign it to the attacher:

attacher = photo.image_attacher
attacher.form_assign({ "image" => file, "title" => "...", "description" => "..." })
attacher.file #=> #<Shrine::UploadedFile> 

It works with remote_url, data_uri, and remove_attachment plugins:

# remote_url plugin 
attacher.form_assign({ "image_remote_url" => "https://example.com/..." })
attacher.file #=> #<Shrine::UploadedFile> 
# data_uri plugin 
attacher.form_assign({ "image_data_uri" => "data:image/jpeg;base64,..." })
attacher.file #=> #<Shrine::UploadedFile> 
# remove_attachment plugin 
attacher.form_assign({ "remove_image" => "1" })
attacher.file #=> nil 

The return value is a hash with form params, with file param replaced with cached file data, which can later be assigned again to the record.

attacher.form_assign({ "image" => file, "title" => "...", "description" => "..." })
#=> { :image => '{"id":"...","storage":"...","metadata":"..."}', "title" => "...", "description" => "..." } 

You can also have attached file data returned as the <name>_data attribute, suitable for persisting.

attacher.form_assign({ "image" => image, ... }, result: :attributes)
#=> { :image_data => '{"id":"...","storage":"...","metadata":"..."}', "title" => "...", "description" => "..." } 
← Cached Attachment DataRemove Attachment →
Shrine
Docs
GuidesPluginsExternalContributing
Community
DiscourseStack Overflow
More
BlogGitHubStar
Follow @shrine_rb
Copyright © 2022 Janko Marohnić