rails-activestorage

Change the ActiveStorage Controller Path

自闭症网瘾萝莉.ら 提交于 2019-11-30 08:22:35
问题 Is there a way to customize the attachment urls so instead of /rails/active_storage/representations/ /rails/active_storage/blobs/ We could have something like this: /custom_path/representations/ /custom_path/blobs/ 回答1: Recently, there was an addition which makes the route prefix configurable: https://github.com/rails/rails/commit/7dd9916c0d5e5d149bdde8cbeec42ca49cf3f6ca Just in master branch now, but should be integrated into ~> 5.2.2 have been integrated into Rails 6.0.0 and higher. Then,

Disable Active Storage in Rails 5.2

风流意气都作罢 提交于 2019-11-30 06:43:07
问题 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? 回答1: Remove next line from config/application.rb require "active_storage/engine" Remove next line from environments config/environments/*.rb config.active_storage.service = :local Remove next line from app/assets/javascripts/application.js //= require activestorage ActiveStorage rails routes will vanish In case there is statement

rails active_storage:install IS NOT WORKING

会有一股神秘感。 提交于 2019-11-30 03:47:14
问题 I have updated my rails api application from 5.1 to 5.2. I am using rails api only. I am trying to use the active storage. I think the problem is due to the line config.api_only = true in config/application.rb . I did lot of google but did not find any thing how to use active storage in rails api. Here is my Gemfile : source 'https://rubygems.org' ruby '2.5.1' git_source(:github) do |repo_name| repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") "https://github.com/#{repo

How to properly do model testing with ActiveStorage in rails?

怎甘沉沦 提交于 2019-11-30 00:21:14
问题 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) { FactoryBot.build(:user) } describe "Upload avatar" do context "with a valid image" do it "saves the image" do valid_user.save! saved_file = valid_user.avatar.attach(io: File.open("/home/ubuntu/workspace/spec/fixtures/files/avatar.jpg"), filename: "face.jpg

Active Storage: Best practice to retain/cache uploaded file when form redisplays

百般思念 提交于 2019-11-29 18:33:56
问题 When uploading files with Active Storage, when a file is finished uploading and the form gets redisplayed, for example when the validation for that form fails for some reason, the file is gone. Is there a way to cache or retain it between form redisplays? Shrine has a nice Plugin for that purpose, I'm looking for something similar for Active Storage. 回答1: Here's a solution to make ActiveStorage files persist on form redisplay: f.hidden_field :image, value: f.object.image.signed_id if f.object

How to compress images before uploading to the cloud using ActiveStorage

江枫思渺然 提交于 2019-11-29 14:34:49
To save space in the cloud how would you go about resizing and compressing an image preupload using activestorage? I tested this code below in development on local storage and it works, but anyway gives some problems that I will explain next. On create this seems to work fine, even if I suppose there should be a cleaner way to do so. class User < ApplicationRecord has_one_attached :avatar before_save :resize_avatar_image def resize_avatar_image filename = avatar.filename.to_s puts attachment_path = "#{Dir.tmpdir}/#{avatar.filename}" File.open(attachment_path, 'wb') do |file| file.write(avatar

Get path to ActiveStorage file on disk

拈花ヽ惹草 提交于 2019-11-29 11:03:00
问题 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 Active Storage Docs, it looked like rails_blob_path would do the trick. After looking at what it returned though, it does not provide the path to the document. Thus, it returns this error: No such file or directory @ rb_sysopen - Background I need

Change the ActiveStorage Controller Path

丶灬走出姿态 提交于 2019-11-29 06:30:06
Is there a way to customize the attachment urls so instead of /rails/active_storage/representations/ /rails/active_storage/blobs/ We could have something like this: /custom_path/representations/ /custom_path/blobs/ Recently, there was an addition which makes the route prefix configurable: https://github.com/rails/rails/commit/7dd9916c0d5e5d149bdde8cbeec42ca49cf3f6ca Just in master branch now, but should be integrated into ~> 5.2.2 and higher. Then, it's just a matter of configuration: Rails.application.configure do config.active_storage.routes_prefix = '/whereever' end Monkey patching is

How to retrieve attachment url with Rails Active Storage with S3

别说谁变了你拦得住时间么 提交于 2019-11-29 02:02:31
rails version 5.2 I have a scenario that I need to access the public url of Rails Active Storage with Amazon s3 to make a zip file with Sidekiq background job. I am having difficulty getting the actual file url. I have tried following rails_blob_url but it gives me following http://localhost:3000/rails/active_storage/blobs/eyJfcmFpbHMiOnsibWVzc2FnZSI6IkJBaHBBZUk9IiwiZXhwIjpudWxsLCJwdXIiOiJibG9iX2lkIn19--9598613be650942d1ee4382a44dad679a80d2d3b/sample.pdf How do I access the real file url through sidekiq ? storage.yml test: service: Disk root: <%= Rails.root.join("tmp/storage") %> local:

Disable Active Storage in Rails 5.2

こ雲淡風輕ζ 提交于 2019-11-28 21:22:56
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? Remove next line from config/application.rb require "active_storage/engine" Remove next line from environments config/environments/*.rb config.active_storage.service = :local Remove next line from app/assets/javascripts/application.js //= require activestorage ActiveStorage rails routes will vanish In case there is statement require 'rails/all' in application.rb then you can use solution provided below where you need to require