Skip to content

Commit 96c7010

Browse files
committed
Add a new example: upload plugin using Active Storage
1 parent 4ec6e80 commit 96c7010

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,32 @@ Why 2 separated scripts/styles? In this way you can include a different version
4747
f.input :description, as: :quill_editor, input_html: { data: { options: { modules: { toolbar: [['bold', 'italic', 'underline'], ['link']] }, placeholder: 'Type something...', theme: 'snow' } } }
4848
```
4949

50+
### ImageUploader plugin
51+
This plugin allows to upload images to the server (instead of storing them in *base64* by default), reference [here](https://github.com/NoelOConnell/quill-image-uploader).
52+
53+
```ruby
54+
# Upload method (to be included in the admin entity configuration)
55+
member_action :upload, method: [:post] do
56+
result = { success: resource.images.attach(params[:file_upload]) }
57+
result[:url] = url_for(resource.images.last) if result[:success]
58+
render json: result
59+
end
60+
```
61+
62+
```ruby
63+
# Form field
64+
unless object.new_record?
65+
plugin_opts = { image_uploader: { server_url: upload_admin_post_path(object.id), field_name: 'file_upload' } }
66+
f.input :description, as: :quill_editor, input_html: { data: { plugins: plugin_opts } }
67+
end
68+
```
69+
70+
For the relevant files of the upload example see [here](examples/upload_plugin_using_activestorage/).
71+
Consider that this is just a basic example: images are uploaded as soon as they are attached to the
72+
editor (regardless of the form submit), it shows the editor only for an existing record (because of
73+
the *upload_admin_post_path*) and it doesn't provide a way to remove images (just deleting them from
74+
the editor will not destroy them, you'll need to implement a purge logic for that).
75+
5076
## Do you like it? Star it!
5177
If you use this component just star it. A developer is more motivated to improve a project when there is some interest.
5278

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# frozen_string_literal: true
2+
3+
ActiveAdmin.register Post do
4+
permit_params :author_id,
5+
:title,
6+
:description,
7+
:category,
8+
:dt,
9+
:position,
10+
:published,
11+
tag_ids: []
12+
13+
member_action :upload, method: [:post] do
14+
result = { success: resource.images.attach(params[:file_upload]) }
15+
result[:url] = url_for(resource.images.last) if result[:success]
16+
render json: result
17+
end
18+
19+
index do
20+
selectable_column
21+
id_column
22+
column :title
23+
column :author
24+
column :published
25+
column :created_at
26+
actions
27+
end
28+
29+
show do
30+
attributes_table do
31+
row :author
32+
row :title
33+
row :description
34+
row :category
35+
row :dt
36+
row :position
37+
row :published
38+
row :tags
39+
row :created_at
40+
row :updated_at
41+
row :images do |resurce|
42+
resurce.images.each do |image|
43+
div do
44+
link_to image.filename, image, target: '_blank'
45+
end
46+
end
47+
nil
48+
end
49+
end
50+
active_admin_comments
51+
end
52+
53+
form do |f|
54+
f.inputs 'Post' do
55+
f.input :author
56+
f.input :title
57+
unless object.new_record?
58+
plugin_opts = { image_uploader: { server_url: upload_admin_post_path(object.id), field_name: 'file_upload' } }
59+
f.input :description, as: :quill_editor, input_html: { data: { plugins: plugin_opts } }
60+
end
61+
f.input :category
62+
f.input :dt
63+
f.input :position
64+
f.input :published
65+
end
66+
67+
f.inputs 'Tags' do
68+
f.input :tags
69+
end
70+
71+
f.actions
72+
end
73+
end
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//= require active_admin/base
2+
3+
//= require activeadmin/quill_editor/quill
4+
//= require activeadmin/quill_editor_input
5+
6+
//= require activeadmin/quill.imageUploader.min
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@import 'active_admin/mixins';
2+
@import 'active_admin/base';
3+
4+
@import 'activeadmin/quill_editor/quill.snow';
5+
@import 'activeadmin/quill_editor_input';
6+
7+
@import 'activeadmin/quill.imageUploader.min';
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
class Post < ApplicationRecord
4+
has_many_attached :images
5+
6+
validates :title, allow_blank: false, presence: true
7+
end

0 commit comments

Comments
 (0)