-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f1133ed
commit 55e19c2
Showing
2 changed files
with
35 additions
and
0 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
docs/2.0/recipes/use-the-has-many-field-for-active-storage-attachments.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Use the `has_many` field for Active Storage attachments | ||
|
||
Because [Active Storage](https://edgeguides.rubyonrails.org/active_storage_overview.html) is implemented as an association. | ||
|
||
For that you should create an `attachment_resource.rb` and configure it like so. | ||
|
||
```ruby | ||
class AttachmentResource < Avo::BaseResource | ||
self.title = :filename | ||
self.model_class = "ActiveStorage::Attachment" | ||
|
||
field :id, as: :id | ||
field :filename, as: :text | ||
field :service_url, as: :external_image, name: "Image" | ||
field :created_at, as: :date_time | ||
end | ||
``` | ||
|
||
Next, add it to your resource that `has_many_attached`: | ||
|
||
```ruby | ||
class Project < ApplicationRecord | ||
has_many_attached :files | ||
end | ||
|
||
class ProjectResource < Avo::BaseResource | ||
# Please make sure you use the _attachments suffix. | ||
# So the :files key from the model file becomes :files_attachments on the resource file. | ||
field :files_attachments, as: :has_many | ||
end | ||
``` | ||
|
||
![](/assets/img/recipes/use-the-has-many-field-for-active-storage-attachments/preview.png) | ||
|
||
We suggest making this resource read-only using Pundit policies so you don't accidentaly update Active Storage keys and settings. |
Binary file added
BIN
+424 KB
...s/img/recipes/use-the-has-many-field-for-active-storage-attachments/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.