How to edit multiple attached images using ActiveStorage `has_many_attached` in ActiveAdmin - ruby-on-rails

I have a simple model that can have multiple images attached via ActiveStorage handling the file storage.
I am using ActiveAdmin to edit my model and to upload/attach the images - so far no problems.
The problem is, when I want to edit my model, and add new images, then the previous ones are deleted, and only the new ones added.
I can do a preview of already attached images, and could also delete them separately, but how do I achieve, that by uploading new images, the old ones are NOT deleted?
My model:
class Post < ActiveRecord::Base
has_many_attached :images
end
My ActiveAdmin page:
ActiveAdmin.register AdminPost do
permit_params images:[]
form do |f|
f.input :images, as: :file, input_html: { multiple: true }
if #resource.images.exists?
#resource.images.map do |m|
para image_tag m
end
end
end
end

Assuming you are using rails 6.0+;
you can solve this by adding following code in to your environments (i.e - development.rb )
https://github.com/rails/rails/issues/35817#issuecomment-628654948
config.active_storage.replace_on_assign_to_many = false
in your form,
form do |f|
f.input :images, as: :file, input_html: { multiple: true }
f.object.images.each do |image|
span image_tag(image)
end
end

So I chose the way of adding the new attachments manually, so they don't replace the existing attached images.
I added a field to handle the posted images, and a method to add them in my model.
class Post < ActiveRecord::Base
has_many_attached :images
attr_accessor :new_images
def attach_images
return if new_images.blank?
images.attach(new_images)
self.new_images = []
end
end
And the ActiveAdmin page controller handles the upload, and calls the new method:
ActiveAdmin.register AdminPost do
permit_params new_images:[]
form do |f|
f.input :new_images, as: :file, input_html: { multiple: true }
if #resource.images.exists?
#resource.images.map do |m|
para image_tag m
end
end
end
controller do
after_save :add_images
def add_images(post)
post.attach_images
end
end
end

Related

ActiveStorage multiple upload with intermediate model

The goal
I want to upload multiple files. So one Intervention can have multiple Uploads and each Upload has one attached file to it. This way, each Upload can have one file attached with different status, name, visibility, etc. instead of having one Upload with has_many_attached
What I have done
I have one Intervention model that can have many uploads :
class Intervention < ApplicationRecord
has_many :uploads, dependent: :destroy
accepts_nested_attributes_for :uploads, :allow_destroy => true
end
Each uploads has one attached file using ActiveStorage :
class Upload < ApplicationRecord
belongs_to :intervention
has_one_attached :file
end
In my interventions_controller I do :
def new
#intervention = Intervention.new
#intervention.uploads.build
end
def create
#intervention = Intervention.new(intervention_params)
# + default scaffolded controller [...]
end
def intervention_params
params.require(:intervention).permit(:user_id, :comment, uploads_attributes: [:status, :file])
end
In my form I have :
<%= form.fields_for :uploads, Upload.new do |uploads_attributes|%>
<%= uploads_attributes.label :file, "File:" %>
<%= uploads_attributes.file_field :file %>
<%= uploads_attributes.hidden_field :status, value: "raw" %>
<% end %>
Problem
This solution works when I want to upload only one file. But if I want to upload two files I can't figure out. I can add multiple: true to the file_field but how to created multiple Upload with one file each ?
Should I save the uploaded files into a temp variable first, extract them from the intervention_params, then create the Intervention without any Upload, then for each uploaded files saved, build a new Upload for the newly created Intervention ?
I have done what I have proposed. I don't know if there is a more elegant way to do it but anyway, this is working.
In my form I just added a simple files field that can take multiple files
<div class="field">
<%= form.label :files %>
<%= form.file_field :files, multiple: true %>
</div>
I have added this to the permitted parameters :
params.require(:intervention).permit(:user_id, :comment, files:[]])
Then I create my Intervention by ignoring this files params and I use it later to create a new Upload record for each files submitted.
# First create the intervention without the files attached
#intervention = Intervention.new(intervention_params.except(:files))
if #intervention.save
# Then for each files attached, create a separate Upload
intervention_params[:files].each do |f|
upload = #intervention.uploads.new()
upload.file.attach(f)
upload.save
end
end

Carrierwave multiple image upload file_field is not enabling selection of multiple images in rails

I am having problems enabling selection of multiple images in implementing Carrierwave multiple images. When I click on the upload button in the new view, I just get the file selection window for a single file and can only select a single file.
In the input form, I have:
<%= simple_form_for #car do |f| %>
...
<%= f.file_field :pictures, multiple: true %>
...
In the Car model, I have:
mount_uploader :pictures, PictureUploader
serialize :pictures, JSON
In the cars controller, I have:
params.require(:car).permit(:name, :make, :year, :color, :seats,
:location, :transmission, :price, :photo, :photo_cache, {pictures: []})
I have a pictures column in the Cars table. I have include cloudinary::Carrierwave in PictureUploader.
Is there anything I'm missing?
At this time multiple image uploads are not supported with Cloudinary’s GEM and Carrierwave integration. However it is on Cloudinary’s road map of implementations. As a workaround for the time being feel free to reference this sample project for multiple uploads using Cloudinary’s GEM and Carrierwave: https://github.com/taragano/Cloudinary_multiple_uploads

Auto fill doesn't work on edit with image uploader

I trying to create a blog with a obligatory image, but after create, when i try to edit this blog, his image appears on thmbnail but not in field and
i have to select the image again.
For upload image i use the carrierwave gem.
My model
class Blog < ActiveRecord::Base
validates :name, :description, :picture, presence: true
mount_uploader :picture, BlogPictureUploader
def picture_url
picture.url
end
My view
= simple_form_for [blog] do |f|
.row
.col-md-6
.panel.panel-default
.panel-heading Foto
.panel-body
.thumbnail class="#{blog.picture? ? "" : "hide"}"
= image_tag blog.picture
p= f.input :picture, label: 'Selecione a foto:', hint: 'A foto deve ter 270x270.'
My controller
class BlogsController < ApplicationController
expose(:blog, attributes: :blog_params)
def update
authorize blog
blog.save
respond_with blog, location: [:blog]
end
The behavior on edit: edit view after click button
If I were you I would create a img element for displaying the image in your edit-page, that was originally uploaded. You don't necessarily have to use the path of the original-image in your input element at the edit-page, and IF the user uploads a new picture it will replace the old one.

Deleting a Paperclip Attachment in Activeadmin

I'm using paperclip to add image attachments to several models and Activeadmin to provide a simple admin interface.
I have this code in my activeadmin model file which allows for image uploads:
form :html => { :enctype => "multipart/form-data"} do |f|
f.inputs "Details" do
f.input :name
f.input :subdomain
end
f.inputs "General Customisation" do
f.input :standalone_background, :hint => (("current image:<br/>").html_safe + f.template.image_tag(f.object.standalone_background.url(:thumb))).html_safe, :as => :file
end
end
which works fine. All of the images I'm attaching like this are optional and so I'd like to give the user the option to remove a previously added image but can't work out how to do this in Activeadmin. All of the example I've seen are for situations where the attachments are managed through a separate has_many association rather than being part of the main model.
Does anyone know of a way to do this?
In your active admin view
form :html => { :enctype => "multipart/form-data"} do |f|
f.inputs "Details" do
f.input :name
f.input :subdomain
end
f.inputs "General Customisation" do
f.input :standalone_background, :hint => (("current image:<br/>").html_safe + f.template.image_tag(f.object.standalone_background.url(:thumb))).html_safe, :as => :file
f.input :remove_standalone_background, as: :boolean, required: false, label: "remove standalone background"
end
end
In your model
You could define a status flag like bellow
attr_writer :remove_standalone_background
def remove_standalone_background
#remove_standalone_background || false
end
OR (depreciated in rails 3.2)
attr_accessor_with_default : standalone_background,false
before_save :before_save_callback
And
def before_save_callback
if self.remove_standalone_background
self.remove_standalone_background=nil
end
end
You could implement this by creating a custom method. This can be done
member_action :custom_action, :method => :get do
//code
end
Also you should add a custom column with a link such as
index do
column "Custom" do |item|
link_to "Custom action", "/admin/items/custom_action"
end
end
Another option is to have a status flag for the attachment or image. Before saving the edited object, you unlink the image.
Thank you for your help guys. This is the final working code...
admin/product.rb
f.input :image, required: false, hint: (("Current image:<br/>").html_safe + f.template.image_tag(f.object.image.url(:thumb))).html_safe
f.input :remove_image, as: :boolean, required: false, label: "Remove Image"
models/product.rb
attr_writer :remove_image
def remove_image
#remove_image || false
end
before_validation { self.image.clear if self.remove_image == '1' }
Although accepts_nested_attributes_for(:foo, allow_destroy: true) only works with ActiveRecord associations like belongs_to we can borrow from its design to have paperclip attachment deletion work in a similar way.
(To understand how nested attributes work in Rails see http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html).
Add a <attachment_name>_attributes= writer method like below to your model that already uses has_attached_file:
has_attached_file :standalone_background
def standalone_background_attributes=(attributes)
# Marks the attachment for destruction on next save,
# if the attributes hash contains a _destroy flag
# and a new file was not uploaded at the same time:
if has_destroy_flag?(attributes) && !standalone_background.dirty?
standalone_background.clear
end
end
The <attachment_name>_attributes= method calls Paperclip::Attachment#clear to mark the attachment for destruction when the model is next saved.
Next open the existing app/admin/your_model_here.rb file (use the correct file path for your app) and setup strong parameters to permit the _destroy flag nested attribute on <attachment_name>_attributes:
ActiveAdmin.register YourModelHere do
permit_params :name, :subdomain,
:standalone_background,
standalone_background_attributes: [:_destroy]
In the same file, add a nested _destroy checkbox to the ActiveAdmin form block. This checkbox must be nested within <attachment_name>_attributes using semantic_fields_for (or one of the other nested attributes methods provided by formtastic).
form :html => { :enctype => "multipart/form-data"} do |f|
f.inputs "Details" do
...
end
f.inputs "General Customisation" do
...
if f.object.standalone_background.present?
f.semantic_fields_for :standalone_background_attributes do |fields|
fields.input :_destroy, as: :boolean, label: 'Delete?'
end
end
end
end
Your form should now show a delete checkbox when there is an attachment present. Checking this checkbox and submitting a valid form ought to delete the attachment.
Source: https://github.com/activeadmin/activeadmin/wiki/Deleting-Paperclip-Attachments-with-ActiveAdmin

Use Carrierwave with Active Admin

Did any of you guys manage to get Active Admin with Carrierwave working?
When I installed AA everything worked fine but the image file upload
fields were plain text fields so added following:
ActiveAdmin.register Club do
form do |f|
f.inputs "Club" do
f.input :league
f.input :name
f.input :image, :as => :file
f.input :approved
end
f.buttons
end
end
Now it's displayed as a file upload field and I can select a file but
after I submitted the form nothing changed. There's still no image and
the image field is empty. Anyone knows what else to do to get it
working?
Finally found the problem.
form do |f|
needs to become:
form(:html => { :multipart => true }) do |f|
I still don't know why console is not working but well, at least I can upload new images now :) Thanks a lot for the help, bruno077!
Yes, it works without an issue, remember to set the attr_accessible if you haven't. According to your configuration, you should have the following code in your model:
#app/models/club.rb
class Club < ActiveRecord::Base
attr_accessible (previous list), :image #If exists
mount_uploader :image, ImageUploader
end
And of course you should have generated the Image uploader with
rails generate uploader image
Edit: you can follow Ryan's railscast if you have any issue. That's what I did for my ActiveAdmin app with Carrierwave

Resources