rails-activestorage

ActiveStorage File Attachment Validation

╄→尐↘猪︶ㄣ 提交于 2019-12-02 19:16:51
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 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 logo.purge errors[:base] << 'Too big' elsif !logo.blob.content_type.starts_with?('image/') logo.purge errors[

Rails 5.2 ActiveStorage undefined method `signed_id' for nil:NilClass

末鹿安然 提交于 2019-12-02 10:12:53
问题 I implemented the code for removing images from my User model. Rails 5.2 Active Storage purging/deleting attachements I think that it removed the image OK, but now I'm getting an error undefined method `signed_id' for nil:NilClass My view ( images.html.erb ): <% if @user.images.attached? %> <% @user.images.each do |image| %> <%= image_tag(url_for(image))%> <%= link_to 'Remove', delete_image_attachment_user_url(image.signed_id), method: :delete, data: { confirm: 'Are you sure?' } %> <% end %>

How is the checksum calculated in the blobs table for rails ActiveStorage

时间秒杀一切 提交于 2019-12-02 06:57:56
Does anyone know how the checksum field in active_storage_blobs is calculated when using ActiveStorage on rails 5.2+? For bonus points, does anyone know how I can get it to use an md5 checksum that would match the one from the md5 CLI command? Lets Break It Down I know i'm a bit late to the party, but this is more for those that come across this in a search for answers. So here it is.. Background: Rails introduced loads of new features in version 5.2, one of which was ActiveStorage . The official final release came out on April 9th, 2018. Rails 5.2 Official Release Notes Disclaimer: So to be

Rails 5.2 ActiveStorage undefined method `signed_id' for nil:NilClass

谁说胖子不能爱 提交于 2019-12-02 03:25:56
I implemented the code for removing images from my User model. Rails 5.2 Active Storage purging/deleting attachements I think that it removed the image OK, but now I'm getting an error undefined method `signed_id' for nil:NilClass My view ( images.html.erb ): <% if @user.images.attached? %> <% @user.images.each do |image| %> <%= image_tag(url_for(image))%> <%= link_to 'Remove', delete_image_attachment_user_url(image.signed_id), method: :delete, data: { confirm: 'Are you sure?' } %> <% end %> <% end %> The controller def images end def delete_image_attachment @image = ActiveStorage::Blob.find

Rails 5.2 ActiveStorage save and then read Exif data

筅森魡賤 提交于 2019-12-02 01:30:11
问题 On Rails 5.2 I am trying to save an avatar via ActiveStorage but it seems as though not image oriantation data is being saved in the active storage blob. I am saving the avatar via a file_field on a create action my #user model has_one_attached :avatar private def avatar_validation if avatar.attached? if avatar.blob.byte_size > 1000000 avatar.purge errors.add(:avatar, 'file is too large') elsif !avatar.blob.content_type.in?(%w[image/png image/jpg image/jpeg]) avatar.purge errors.add(:avatar,

Rails 5.2 ActiveStorage save and then read Exif data

99封情书 提交于 2019-12-01 21:11:53
On Rails 5.2 I am trying to save an avatar via ActiveStorage but it seems as though not image oriantation data is being saved in the active storage blob. I am saving the avatar via a file_field on a create action my #user model has_one_attached :avatar private def avatar_validation if avatar.attached? if avatar.blob.byte_size > 1000000 avatar.purge errors.add(:avatar, 'file is too large') elsif !avatar.blob.content_type.in?(%w[image/png image/jpg image/jpeg]) avatar.purge errors.add(:avatar, 'file type needs to be JPEG, JPG, or PNG') end end end I have been reading some documentation for

How to query records that have an ActiveStorage attachment?

大憨熊 提交于 2019-12-01 15:57:49
问题 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 using joins to return all Users with an attachment, but this does not seem to work on either the blob or attachment table, or perhaps I'm not getting the syntax correct. I'm sure I am overlooking something obvious. Is it possible to do something along the

Activestorage fixtures attachments

给你一囗甜甜゛ 提交于 2019-12-01 15:22:11
问题 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: <%= File.open Rails.root.join('test', 'files', 'image.png').to_s %> How do I properly attach an avatar file through fixtures? 回答1: Lets say you have a test for the model user, the default UserTest#test_the_truth rails test test/models/user_test.rb I suppose you are

rails active_storage:install IS NOT WORKING

半城伤御伤魂 提交于 2019-11-30 20:08:01
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_name}.git" end gem 'rails', '~> 5.2.0' gem 'pg', '>= 0.18', '< 2.0' gem 'puma', '~> 3.7' gem 'rack-cors'

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

橙三吉。 提交于 2019-11-30 12:59:57
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. 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.image.attached? f.file_field :image 来源: https://stackoverflow.com/questions/50360307/active-storage-best