Questions tagged [rails-activestorage]

Active Storage is a component of Ruby on Rails. It facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects.

Official documentation can be found in the Rails Guides.

1229 questions
52
votes
4 answers

Get path to ActiveStorage file on disk

I need to get the path to the file on disk which is using ActiveStorage. The file is stored locally. When I was using paperclip, I used the path method on the attachment which returned the full path. Example: user.avatar.path While looking at the …
Neil
  • 4,578
  • 14
  • 70
  • 155
46
votes
9 answers

How to properly do model testing with ActiveStorage in rails?

I have just switched to using ActiveStorage on rails 5.1.4 and I am new to TDD and struggling to figure out how to test a model that has_one_attached :avatar require 'rails_helper' RSpec.describe User, :type => :model do let (:valid_user) {…
Donato Azevedo
  • 1,378
  • 1
  • 13
  • 22
37
votes
8 answers

How to retrieve attachment url with Rails Active Storage with S3

rails version 5.2 I have a scenario where I need to access the public URL of Rails Active Storage with Amazon S3 storage to make a zip file with Sidekiq background job. I am having difficulty getting the actual file URL. I have tried rails_blob_url…
Shani
  • 2,433
  • 2
  • 19
  • 23
37
votes
6 answers

How do I duplicate a file stored in ActiveStorage in Rails 5.2

I have a model that is using ActiveStorage: class Package < ApplicationRecord has_one_attached :poster_image end How do I create a copy of a Package object that contains a duplicate of the initial poster_image file. Something along the lines…
Robban
  • 1,191
  • 2
  • 13
  • 25
36
votes
4 answers

Disable Active Storage in Rails 5.2

Upgrading Rails to 5.2, and I found out that I must commit the storage.yml into version control. I don't plan to use ActiveStorage. Is there a way to disable it?
lulalala
  • 17,572
  • 15
  • 110
  • 169
35
votes
4 answers

How to query records that have an ActiveStorage attachment?

Given a model with ActiveStorage class User has_one_attached :avatar end I can check whether a single user has an avatar @user.avatar.attached? But how can I return a collection of all users with (or all users without) an attachment? I tried…
Andy Harvey
  • 12,333
  • 17
  • 93
  • 185
34
votes
3 answers

undefined method 'service_name' for ActiveStorage::Blob after upgrading to rails 6.1.0

I upgraded my app from 6.0.3.4 to 6.1.0 and got this error message. Looked it up and didn't find anything. I have a very basic usage of ActiveStorage
Dorian
  • 7,749
  • 4
  • 38
  • 57
34
votes
7 answers

How to get url of Active Storage image

I want to get list of records with attached images as a links or files by api. I have a simple model: class Category < ApplicationRecord has_one_attached :image validates :name, presence: true, uniqueness: true end And next action: def index …
zishe
  • 10,665
  • 12
  • 64
  • 103
32
votes
5 answers

Activestorage fixtures attachments

In rails tests. I have a basic model with only activestorage: class User < ApplicationRecord has_one_attached :avatar end I'm trying to make it's fixture, but having no luck with (I do have an image there): # users.yml one: avatar: <%=…
joseramonc
  • 1,811
  • 2
  • 21
  • 40
32
votes
2 answers

Rails 5.2 Active Storage add custom attributes

I have a model with attachments: class Project < ApplicationRecord has_many_attached :images end When I attach and save the image I also want to save an additional custom attribute - display_order (integer) with the attached image. I want to use…
paxer
  • 961
  • 10
  • 17
29
votes
4 answers

How to save an image from a url with rails active storage?

I'm looking to save a file (in this case an image) located on another http web server using rails 5.2 active storage. I have an object with a string parameter for source url. Then on a before_save I want to grab the remote image and save…
Ed_
  • 1,676
  • 2
  • 13
  • 22
29
votes
7 answers

ActiveStorage File Attachment Validation

Is there a way to validate attachments with ActiveStorage? For example, if I want to validate the content type or the file size? Something like Paperclip's approach would be great! validates_attachment_content_type :logo, content_type:…
Tom Rossi
  • 11,604
  • 5
  • 65
  • 96
26
votes
2 answers

Heroku: Couldn't find Active Storage configuration in /app/config/storage.yml (RuntimeError)

My app deploys to Heroku but crashes every time. I don't know why. I have set up Carrierwave, fog, and aws for an app in production on Heroku before just fine. Tried to follow the same steps and I am getting an h10 error code. In the rails console…
23
votes
1 answer

How do you change the Active Storage Service url_expires_in timeout?

When Active Storage creates a signed variant URL, it sets a default timeout of 5.minutes. I really want to increase this, but I've been trawling Github issues, code diving and cannot find it anywhere. On line 44 of the services class a…
Paul Danelli
  • 994
  • 1
  • 15
  • 25
23
votes
3 answers

Using multiple buckets with ActiveStorage

Does anyone know if there is a way to configure custom buckets for specific attachments? class MyModel < ApplicationRecord ... has_one_attached :logo, bucket: 'custom_bucket' ... end
Tom Rossi
  • 11,604
  • 5
  • 65
  • 96
1
2 3
81 82