Skip to content

Commit 1bfd52b

Browse files
First pass on documentation for using Shrine for attachments (#154)
1 parent 9a1ec1b commit 1bfd52b

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
---
2+
title: Using Shrine for attachments
3+
permalink: /use-shrine-for-attachments/
4+
---
5+
6+
[Shrine](https://shrinerb.com) is a great toolkit for file attachments; and with some modifications, you can use it instead of ActiveStorage!
7+
8+
<%= render Alert.new(type: "primary") do %>
9+
For this example, we're using Shrine's [Upload Endpoint](https://shrinerb.com/docs/plugins/upload_endpoint) plugin on the server.
10+
<% end %>
11+
12+
The process is:
13+
1. Setup the endpoint on your server that will accept the attachment
14+
2. Add the `data-direct-upload-url` attribute, which points to the endpont
15+
3. Add an event listner for `rhino-attachment-add` that uploads the file to your endpoint, then complete the attachment
16+
add process with the appropriate calls to `event.attachment`
17+
18+
19+
<%= render Syntax.new("js") do %>
20+
this.addEventListener(`rhino-attachment-add`, async function(event) {
21+
event.preventDefault()
22+
const { attachment, target } = event;
23+
24+
const url = event.target.dataset.directUploadUrl
25+
26+
let formData = new FormData()
27+
formData.append('file', attachment.file, attachment.file.name)
28+
29+
let response = await window.mrujs.fetch(url, {
30+
method: 'POST',
31+
body: formData,
32+
headers: {"Accept": "application/json"}
33+
});
34+
35+
let result = await response.json();
36+
37+
attachment.setAttributes({
38+
url: result.url,
39+
});
40+
41+
attachment.setUploadProgress(100)
42+
})
43+
<% end %>

0 commit comments

Comments
 (0)