Use Carrierwave with Active Admin - ruby-on-rails

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

Related

How to edit multiple attached images using ActiveStorage `has_many_attached` in ActiveAdmin

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

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

Rails direct upload to Amazon S3 using Activeadmin + Paperclip

I am using Activeadmin and Paperclip to make images upload on my Rails app. When I try to upload big files to S3 the timeout error occurs, so I have to implement the direct upload to S3.
Does anyone know how can I make it? I could't figure it out...
There is a really nice article I've used when was first time setting up the AA+s3+Paperclip.
It has decent explanations + example app on Github, so you can check it live.
In AA the form would look something like this:
form multipart: true do |f|
# f.semantic_errors *f.object.errors.keys
f.inputs do
f.input :image_name #or whatever field is called
end
f.has_many :attachments do |a|
if a.object.persisted?
link_to image_tag(a.object.encoded_url, class: 'image-preview'), a.object.encoded_url, target: "_blank"
else
a.inputs do
a.s3_file_field(:attachment, as: :file, class: 'js-s3_file_field')
end +
a.inputs do
a.input(:s3_url, as: :hidden, input_html: { class: "s3_url" })
end
end
end
f.actions
end
The answer appears to be in the comments. Thanks Andrey for the tutorial link.
http://blog.littleblimp.com/post/53942611764/direct-uploads-to-s3-with-rails-paperclip-and

ActiveAdmin, Formtastic, and Paperclip: Not Rendering File Dialog

I'm implementing a generic media gallery using Ruby on Rails. I've chosen ActiveAdmin to handle the administration portion of my task and it's worked well so far, except for one thing: It's not displaying the "Choose file" dialog as intended.
This is a form for my "Media" section of ActiveAdmin. I have a model called "Medium" with the following fields (in addition to id and timestamp:
asset_file_name
asset_file_size
asset_content_type
asset_updated_at
My Medium model looks like this:
class Medium < ActiveRecord::Base
has_and_belongs_to_many :galleries
has_and_belongs_to_many :entities
has_attached_file :asset, :styles => { :medium => "300x300>", :thumb => "100x100>" }
attr_accessible :asset
end
And I'm adding it to the ActiveAdmin form like this:
form :html => { :enctype => "multipart/form-data" } do |f|
f.input :asset, :as => :file
f.buttons
end
Here's a screencap of my ActiveAdmin page:
I see nothing wrong with how I'm implementing this. I've read that Formtastic has historically had issues with paperclip and I'm not averse to switching to attachment_fu or any other suitable solutions.
I should also note: I know that I can add in a custom partial. It's not my ideal solution, as I'd like to keep everything in the Formtastic DSL.
Thanks!
Formtastic requires that you wrap all calls to #input in a call to #inputs. It's definitely something that I would like to see fixed in Active Admin.
It should work if you wrap your input in a call to inputs:
form :html => { :enctype => "multipart/form-data" } do |f|
f.inputs do
f.input :asset, :as => :file
end
f.buttons
end
Let me know if this works for you.
Or you can do:
form :html => {:multipart => true} do |f|
which is easier to remember, imho.
the latest active admin handle it automatic
I use carrier wave with active admin and works as above.

Image file input with Formtastic and ActiveAdmin

I started to use formstatic but I need to make a file field with image preview. I mean, when i edit an object, i want to see the image already linked.
How can I do that?
Thank you !
The answer is to use the hint attribute :
ActiveAdmin.register Event do
form :html => { :enctype => "multipart/form-data" } do |f|
f.input :map, :as => :file, :hint => f.template.image_tag(f.object.map.url(:thumb))
end
end
Bye
Use paperclip with formtastic
Formtasitc's github page mentions that it supports paperclip:
:file – a file field. Default for file-attachment attributes matching: paperclip or attachment_fu.
Here are some useful screencasts that will get you going:
Paperclip
Cropping images
EDIT:
To display an image in a column of a grid in ActiveAdmin you need to make a custom column (This is untested and could be flawed, I'm extrapolating this from the documentation):
index do
column "Title" do |post|
link_to image_tag("path to file", :alt => "post image"), admin_post_path(post)
end
end
Two Gems and one plugin can help your case:
Make sure you look at:
Gems:
Paperclip: https://github.com/thoughtbot/paperclip
RailsCast on PaperClip: http://railscasts.com/episodes/134-paperclip
CarrierWave: https://github.com/carrierwaveuploader/carrierwave
RailsCast on CarrierWave: http://railscasts.com/episodes/253-carrierwave-file-uploads
Jquery File Upload: https://github.com/blueimp/jQuery-File-Upload
Jquery File Upload RailsCast: http://railscasts.com/episodes/381-jquery-file-upload (Need a Pro Account for RailsCast)
As #ianpetzer said, in Rails 4.2 / ActiveAdmin master the current answer causes an object reference to be written out as well. The correct answer for 2016 should be similar to this answer:
form :html => { :multipart => true } do |f|
f.inputs do
#...
f.input :image, required: false, hint: image_tag(object.image.url(:medium)).html_safe
#...
end
end

Resources