Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Paperclip with Rails4 and active admin
ActiveAdmin.register Project do
# Don't forget to add the image attribute (here thumbnails) to permitted_params
controller do
def permitted_params
params.permit project: [:title, :summary, :description, :thumbnail, :date, :url, :client_list, :technology_list, :type_list]
end
end
form do |f|
f.inputs "Project Details" do
f.input :title
f.input :thumbnail, :as => :file, :hint => f.template.image_tag(f.object.thumbnail.url(:thumb))
# Will preview the image when the object is edited
end
f.actions
end
show do |ad|
attributes_table do
row :title
row :thumbnail do
image_tag(ad.thumbnail.url(:thumb))
end
# Will display the image on show object page
end
end
end
class Project < ActiveRecord::Base
has_attached_file :thumbnail, :styles => { :medium => "300x300#", :thumb => "200x200#" }
# 200x200# crop the image
# 200x200> scale the image to 200px wide at maximum keeping aspect ratio
end
@KanybekMomukeyev
Copy link

KanybekMomukeyev commented Apr 2, 2014

thanks for sharing, but have some errors here:

f.template.image_tag(f.object.thumbnail.url(:thumb))

Showing /.rvm/gems/ruby-2.1.1/bundler/gems/active_admin-ee996b09edb9/app/views/active_admin/resource/new.html.arb where line #1 raised:

wrong number of arguments (1 for 0)
Extracted source (around line #1):
1
insert_tag renderer_for(:new)

@KanybekMomukeyev
Copy link

KanybekMomukeyev commented Apr 2, 2014

YEAH!
Finally get it worked:
Above in "admin-projects.rb" file, 13 line change into:

f.input :thumbnail, :required => false, :as => :file

Also in "project.rb", add extra line :

validates_attachment :thumbnail, content_type: { content_type: ["image/jpg", "image/jpeg",     "image/png"] } 

@scarver2
Copy link

scarver2 commented Jul 25, 2014

Thanks for sharing, @jameslafa. The protected params can be written in one line. It doesn't require the controller wrapper or permitted_params method.

permit_params :title, :summary, :description, :thumbnail, :date, :url, :client_list, :technology_list, :type_list

@vuongnd
Copy link

vuongnd commented Jun 17, 2016

thanks for sharing .... It too simple after I read this doc

@skad0
Copy link

skad0 commented Jun 23, 2016

I think, f.template on str 13 is unnecessary and may cause weird output like <Class:..>.
It should be
hint: f.template.image_tag(f.object.thumbnail.url(:thumb))

@turkoz
Copy link

turkoz commented Sep 8, 2016

Thanks for your sharing. I am getting some weird message in edit form while using f.template.image_tag. I suggest using image_tag like below:
hint: image_tag(f.object.thumbnail.url(:thumb))

@Preen
Copy link

Preen commented Feb 6, 2017

@turkoz yes! Works nice

@bhfailor
Copy link

bhfailor commented Aug 16, 2017

Your comments were all very useful for me - I thank you.

@paraskotadiya
Copy link

paraskotadiya commented Sep 26, 2017

hyy i need help Active Admin image uploading for product please help me ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment