rails-activestorage

Rails 6 Active Storage : Could not find or build blob: expected attachable, got nil

只愿长相守 提交于 2020-01-24 15:49:05
问题 Just created a new Rails 6 app, and I am trying to allow adding images to an active_storage blob instead of replacing them, through a form generated with rails scaffold. Followed the documentation (https://guides.rubyonrails.org/active_storage_overview.html#has-many-attached), using #attach into my controller, but it leads to an error page and keep the "default" behavior of replacing all the images instead of adding new images. Using Rails 6.0.0 with active_storage 6.0.0 I first made a Page

Download an active Storage attachment to disc

耗尽温柔 提交于 2020-01-24 02:50:08
问题 The guide says that I can save an attachment to disc to run a process on it like this: message.video.open do |file| system '/path/to/virus/scanner', file.path # ... end My model has an attachment defined as: has_one_attached :zip And then in the model I have defined: def process_zip zip.open do |file| # process the zip file end end However I am getting an error : private method `open' called on the zip.open call. How can I save the zip locally for processing? 回答1: As an alternative in Rails 5

Active Storage raises ActiveSupport::MessageVerifier::InvalidSignature

感情迁移 提交于 2020-01-13 10:43:13
问题 To import an image file into the Rails app using Active Storage, I wrote a Rake like this: task :import_file => :environment do path = Rails.root.join("tmp", "sample.jpg") data = File.read(path) post = Post.first post.image.attach(data) end When I executed this task, I got an Exception ActiveSupport::MessageVerifier::InvalidSignature . How can I avoid this error? The source code of Post model is: class Post < ApplicationRecord has_one_attached :image end I use the default config/storage.yml .

ActiveStorage big file uploads triggers Google::Execution::Expired

≯℡__Kan透↙ 提交于 2020-01-06 06:05:51
问题 While implementing ActiveStorage at work we found out that when uploading big file, 12GB , the operations hangs for about 10 minutes and the I get the error Google::Execution::Expired or sometimes HTTPClient::SendTimeoutError: execution expired . I am running most uploads with a line like this: backup.file.attach(io: File.open("/my/file/path.ext"), filename: "myfilename") Is there a way to make the request to last longer or a way to circunvent this timeouts. This strategy has worked fine, so

How to upload a folder using Activestorage while maintaining original folder structure

99封情书 提交于 2020-01-06 04:34:25
问题 I am looking for a way to upload whole folder using activestorage using its direct_upload feature but I am not able to retrieve file's relative path(webkitRelativePath) on rails end so that I can create exact same structure on S3 or on local disk. Here the code I am using <%= form_tag('/action', method: "post", id:"myForm") do %> <%= file_field_tag(:file, webkitdirectory: '', directory: '', multiple: true, direct_upload: true) %> <%= submit_tag("Submit", id:"submit_btn") %> <% end %> Any help

ActiveStorage File Attachment Validation

时光怂恿深爱的人放手 提交于 2019-12-31 08:46:52
问题 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: /\Aimage\/.*\Z/ validates_attachment_size :logo, less_than: 1.megabytes 回答1: Well, it ain't pretty, but this may be necessary until they bake in some validation: validate :logo_validation def logo_validation if logo.attached? if logo.blob.byte_size > 1000000

Rails 5 ActiveStorage How to wait for all threads to finish

与世无争的帅哥 提交于 2019-12-25 00:34:46
问题 First, let me describe the problem. I'm using RSpec to run a script to generate some test data. RAILS_ENV=development rspec spec/dev/food/food_upload.rb The script looks like this. image = fixture_file_upload(Rails.root.join('spec', 'resources', 'test2.png'), 'image/png') (0..200).each { food = FactoryBot.build :food p "Loading #{food.name}" expect { post :upload, params: { "foodUpload_foodName"=> food.name, "foodUpload_image"=> image } }.to change(Food, :count).by(1) .and change(Image,

Reading from Active Storage Attachment Before Save

廉价感情. 提交于 2019-12-24 20:05:15
问题 I have users uploading JSON files as part of a model called Preset , very standard Active Storage stuff. One thing that's somewhat out of the ordinary (I suppose, given my inability to make it work) is that I'd like to grab data from the uploaded JSON file and use it to annotate the Preset record, like so: class Preset < ApplicationRecord has_one_attached :hlx_file before_save :set_name def set_name file = JSON.parse(hlx_file.download) self.name = file['data']['name'] end end When I call hlx

undefined method `signed_id' for nil:NilClass

不羁的心 提交于 2019-12-23 03:03:08
问题 A while back I tried to delete attachments of the active storage so I follow the answer of this question: Rails 5.2 Active Storage purging/deleting attachements But after doing this: def delete_image_attachment @image = ActiveStorage::Blob.find_signed(params[:id]) @image.purge redirect_to collections_url end I start getting this error every time I entered a view with images or a link to the destroy method: So I change the code a bit: routes.rb match 'vehicles/:id/:image_id' => 'vehicles

When using activestorage in Rails 6, how do I retain a file when redisplaying a form?

蓝咒 提交于 2019-12-22 18:03:08
问题 In Rails 6 I have a form with a file field and I am using activestorage to store the file. If validations fail after submitting the form, the form is redisplayed showing the validation errors. How can I retain the file added to the file field when redisplaying the form so that the user does not have to add the file again to the form? There is already a similar question for rails 5: Active Storage: Best practice to retain/cache uploaded file when form redisplays, however the solution there