Setting up cloudinary and paperclip - ruby-on-rails

I used the paperclip gem to upload images onto my app. Now I'm trying to deploy to Heroku and want to use Cloudinary to host the images. I followed the documentation and added my cloudinary.yml file into my config directory. Even locally when I add this my images disappear
this works locally:
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100" }
Adding the commands for cloudinary makes all my images disappear:
has_attached_file :avatar, styles: { medium: "300x300", thumb: "100x100" },:storage => :cloudinary,
:path => ':id/:style/:filename'
If I try to push to heroku it also doesn't work.

Related

Rails and Paperclip, default_url not working

I'm using paperclip for uploading profile pictures. When someone does not upload an image I want a default image to be assigned to the user instead.
I'm using this line of code:
has_attached_file :avatar,
styles: { medium: "300x300>", thumb: "100x100>" },
default_url: "assets/images/:style/male.jpg"
But my browser inspector gives me this error:
http://localhost:3000/assets/images/original/male.jpg 404 (Not Found)
I've tried writing:
default_url: "assets/images/:style/male.jpg"
default_url: "images/:style/male.jpg"
default_url: ":style/male.jpg"
default_url: "male.jpg"
default_url: "assets/images/male.jpg"
The image lies in the following places:
/assets/images/male.jpg
/assets/images/medium/male.jpg
/assets/images/thumb/male.jpg
/assets/images/original/male.jpg
Ommit the /images/ instead do this /assets/male.jpg or /assets/original/male.jpg
Hope that help you out.

Image Paperclip::Errors::NotIdentifiedByImageMagickError #Windows

I'm getting tired of this error...
I tried everything that's on the internet (that I found so far)
Gem
gem 'paperclip', '~> 4.3', '>= 4.3.6'
config/environments/development.rb
Paperclip.options[:command_path] = "/c/Program Files/ImageMagick-7.0.1-Q16/"
Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'
path for ImageMagick/convert
https://gyazo.com/2e8714546606b796b63f5b64663cab31
file.exe it installed
https://gyazo.com/5d0d3d5723c52e6cc812d72202ba4038
my model
has_attached_file :image, styles: { medium: "300x300>"}
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
This might be a little late.
Lots of people with this issue are running Windows.
Image Magick should be installed correctly (all options selected during install). It is a big step to reinstall and reboot so leave this option until last.
Open config/environments/development.rb
Add the following line: `Paperclip.options[:command_path] = 'C:\Program Files (x86)\GnuWin32\bin'. You check the path (mine is shown) by running which convert at the command line. Not the command line that ships with windows. I run git bash.
Restart your Rails server
Check your model. My syntax in app\models\post.rb was
has_attached_file :image, styles: { medium: "300x300>", thumb: "100x100>" }
which I changed to
has_attached_file :image, style: { :medium => "300x300>", :thumb => "100x100>" }
You may find the error goes but the image is not showing. Check your code in the show page.
If I remove (:medium) the image shows fine.
It doesn't show when I leave the code like below (Haml, sorry!)
= image_tag #recipe.image.url(:medium), class: "recipe_image"

Getting URL for public assets in Rails model

I have a Rails application that uses Paperclip and saves images to S3. When the user uploads an asset without an image, it gets the default image set in the Paperclip setup.
My API serves those assets and has the links to the images in the JSON response (using jbuilder), however I can't seem to return the default image URL, it only returns "missing.png" and I wanted it to return the entire URL to the server with the missing image path attached to it.
I'm setting the default url in the model, and I've tried using ActionView::Helpers::AssetUrlHelper to get the image_url but it never works even though it is working inside the rails console. Any idea on what can I do to solve it?
The JBuilder file:
json.profile_picture_smallest asset.profile_picture.url(:smallest)
json.profile_picture_small asset.profile_picture.url(:small)
json.profile_picture_medium asset.profile_picture.url(:medium)
json.profile_picture_large asset.profile_picture.url(:large)
json.profile_picture_original asset.profile_picture.url(:original)
The part of paperclip that is included in the Model
module Picturable
extend ActiveSupport::Concern
included do
has_attached_file :profile_picture, path: '/images/' + name.downcase.pluralize + '/:style/:basename', default_url: "missing.png",
styles: {
smallest: '50x50>',
small: '100x100>',
medium: '200x200>',
large: '400x400>',
png: ['400x400>',:png]
}, :convert_options => {
smallest: '-trim',
small: '-trim',
medium: '-trim',
large: '-trim',
png: '-trim'
}
# Validate the attached image is image/jpg, image/png, etc
validates_attachment_content_type :profile_picture, :content_type => /\Aimage\/.*\Z/
end
def set_uuid_name
begin
self.profile_picture_file_name = SecureRandom.uuid
end while self.class.find_by(:profile_picture_file_name => self.profile_picture_file_name)
end
end
Paperclip config:
Paperclip::Attachment.default_options[:s3_host_name] = 's3hostname'
Development config:
config.paperclip_defaults = {
:storage => :s3,
:s3_credentials => {
:bucket => 'paperclipdev',
:access_key_id => 'accesskey',
:secret_access_key => 'secretaccesskey'
}
}
I think the way to do this is use the asset helpers in your jbuilder file:
json.profile_picture_smallest asset_url(asset.profile_picture.url(:smallest))
It's worth a mention here that you can also pass a symbol method name to paperclip for the default_url parameter if you want the default url to be dynamic based on the model.

Ruby on rails gem paperclip How to generate missing image

I uploaded a missing.png to public/images/ folder. But the the missing.png not showing correctly.
The website requires images/small/missing.png
So I guest I should generate the missing.png for thumb small medium large size.
What should I do?
Update 1:
I manually create a folder public/images/small and put missing.png inside the small folder. The website shows the missing.png. But What's is the correct way to generate all size of missing.png?
Try to add this in your model:
has_attached_file :image, styles: { :small => "150x150>", medium: "300x300>", thumb: "100x100>" }, default_url: "/images/:style/missing.png"

Is there anything wrong in putting default_url paperclip images in the public directory of a Rails 4.1 app?

I have was seeing several discussions (on stackoverflow here and here, and as a bug on paperclip here) around the best way to tell the default_url of images using Paperclip and Rails so that they work fine in production with the asset pipeline. All solutions appear quite complicate.
Is there anything wrong in putting the default images in the public/ directory of the Rails app? Anything I need to worry down the line or that I am missing?
If I put images in the public/ directory and access them with the code below all appears to work correctly.
has_attached_file :image,
styles: {original: "1000x1000", medium: "530x530#", thumb: "300x300#"},
default_url: "/default-avatar_:style.png"
I generally, by convention alone include the missing styles images in the assets directory. You don't need any extra coding or complex mechanisms to have paperclip utilize them.
#Images within assets:
/app/assets/images/default-avatar_original.png
default-avatar_medium.png
default-avatar_thumb.png
# paperclip config in /app/models/user.rb
has_attached_file :image, :styles => {:original => ["1000x1000", :png],
:medium => ["530x530#", :png],
:thumb => ["300x300#", :png] },
:default_url => "/assets/default-avatar_:style.png"
Note that when specifying default_url, you exclude the /image/ directory.

Resources