Carrierwave - show thumbnail in ActiveAdmin after upload with remove/delete link - carrierwave

I use ActiveAdmin to upload featured images to posts. It works but after I save the post, and click on edit, the image field is empty, although the image is still attached to it.
What I want to achieve is after upload I would like to show the thumbnail of the image below the image upload field, with an option to remove/delete the image (preferably without touching the save button).
I also use the activeadmin-wysihtml5 gem along with the activeadmin-dragonfly gem which handles image uploads when I want to insert images in the body of the post through the editor.
These gems also created an Assets link in ActiveAdmin, where I can see and handle all the uploaded images. Is it possible to do this with Carrierwave? Or maybe make Carrierwave use the existing assets and upload there?
Best Wishes,
Matt

The answer to this is featured on ActiveAdmin Wiki as Showing an uploaded image in the form
f.inputs "Attachment", :multipart => true do
f.input :cover_page, :as => :file, :hint => f.object.cover_page.present? \
? f.template.image_tag(f.object.cover_page.url(:thumb))
: f.template.content_tag(:span, "no cover page yet")
f.input :cover_page_cache, :as => :hidden
end

Related

How to add a picture to a model (User) in Rails using ActiveStorage and the Cloudinary Upload Widget?

world!
After implementing Cloudinary's Active Storage Integration, it's easy to make it work in a form.
We just need to add to our simple_form_for a
<%= f.input :photo, as: :file %>
BUT, if we want to use the Cloudinary Upload Widget, there's no straightforward way of adding this file to the form.
What I have tried is:
1- let the widget upload the picture;
2- update the value of a hidden input with the url of the uploaded picture;
3- in the controller, when creating the model, download the picture and attach it to the model:
require 'open-uri'
file = URI.open(photo_url)
#user.photo.attach(io: file, filename: 'profile_photo.jpg')
# [...]
private
def photo_url
params.require(:user).permit(:photo_url)[:photo_url]
end
It works, BUT it uploads twice the same picture and - IMHO - is extremely inefficient.
Any ideas?
Thank you!
Good Luck, Have Fun!

How to show image file when i select it in file browser , Carrierwave gem

I am using Carrierwave gem for image uploading. i want to achieve image upload and show after uploading completed.
i read the wiki of this gem and used the same code but not working.
= form_for #user, html: { :multipart => true, class: 'battle_form' } do |f|
= image_tag(#user.file_attachment1_url) if #user.file_attachment1?
= f.file_field :file_attachment1, class:'fileInput', name:'image1'
= f.hidden_field :file_attachment1_cache
Carrierwave Gem GITHUB
If you want to show image after upload, just use img tag and you upload file url like:
= image_tag(#image_url)
This will work after upload completed and refresh current page.
If you just want to preview this image before form submit, you may find some way with javascript to solve it, like dropzonejs may help you

Paperclip image medium size broken to missing link

I am doing the One Month Rails Course. I am using Rails 4. My Github folder is: https://github.com/phanatuan/pinteresting
I use the Paperclip gem. In the App/views/pins/show.html.erb, I want to show the pins picture at medium size. I passed the code <%= image_tag #pin.image.url %>. The browser shows the pin with the un-resized pictures. When i pass ":medium" into the code as such <%= image_tag #pin.image.url(:medium) %>, the browser shows the missing image.
I check all the documentations and follow closely the instructions but I could not find the mistake.
Really Appreciate your help
Tuan
Its hard to diagnose this without seeing the code, but my best guess is that you did not declare the style :medium for the attached file in your model.
Try this:
has_attached_file :image,
:styles => { :medium => "120x120>" }
Also, make sure that you specify a :style in your path declaration for the file.

Use PDF as an image Rails

I'm using paperclip to upload my images, in this instance i would like to use the pdf as an image and enable the user to have the option of downloading it. I upload my images to an S3 bucket.
Currently when trying to render the image it fails to load the given URL
<% for i in #timetable %>
<%= image_tag(i.photo.url(:timetable)) %>
<% end %>
Whereas the exact same code works if the image type is png or jpeg for example
The HTML generated is
<img src="http://ymcagym.s3.amazonaws.com/images/timetables/13/timetable.pdf?1392893849" alt="Timetable">
When putting the link in the url it renders but opens in a pdf reader
How would i go about getting it to open as an image? and also having the option to download
The
Thanks
You can enforce the file format when specifying your styles, for example:
has_attached_file :photo,
:styles => {
:timetable => ['100x100#', :jpg],
...
}
this should create a preview image of the first page for you on upload. Be sure to run rake paperclip:refresh to regenerate your assets.

Rails - HowTo - Handle Users w/o Images

I just installed paper_clip to allow user's to upload a profile pic which can be displayed easily with:
<%= image_tag #user.profile_pic.url %>
What I'm curious about now is how to handle user's that don't have a profile_pic uploaded.. How to show a standard site user image (generic)
Should I make a helper?
something like showProfilePic(#user, size)
And then use that helper to show the right size image, either the user's uploaded photo or a generic site profile pic?
thanks. Any existing helpers out there?
You can use the default_url and default_style options to set this. The default default_url is /:attachment/:style/missing.png, so you could drop in missing.png for each style and have that be your generic profile pic. If you want to customize, though...
class User < ActiveRecord::Base
has_attached_file :profile_pic,
:default_style => :thumbnail,
:default_url => '/path/to/:style_default.jpg'
end

Resources