I have two models. One being Image which has an attachment called "file" and saves to "/photos". These would be simple images that would be inside of, say, an album. I also have users that have an attachment called "avatar" which saves to "avatars/etc etc". How can I copy one attachment (#image.file) over to my user to save it as his avatar? I want the user to be able to select an image that he/she has in their album images and use it as an avatar. I want to copy it though so if the user ever deletes that image from their album, it won't delete their avatar.
User Model Snippet:
has_attached_file :avatar, :styles => {
:huge => '220x220!',
:full => '72x72!',
:medium => '48x48!',
:small => '24x24!'
},
:path => ':rails_root/public/avatars/:userid_prefix/:userid/:style-:random.:extension',
:url => '/avatars/:userid_prefix/:userid/:style-:random.:extension',
:default_url => :default_avatar_url
Image Model Snippet
has_attached_file :file,
:styles => {
:large => '640x480',
:avatar => '72x72!',
:cover => '160x160!'},
:path => ':rails_root/public/photos/:userid_prefix/:userid/:id_:style-:random.:extension',
:url => '/photos/:userid_prefix/:userid/:id_:style-:random.:extension'
Perhaps not the most efficient approach in the world, but you could have Paperclip import the image from the URL on your site.
Let's say you present the user with their photos, and whichever one they click calls User#update with params => { :avatar_url => 'http://www.yourownwebsite.com/url/of/image.jpg' }.
You then process this param in the controller doing something like:
if params.has_key? :avatar_url
begin
#user.avatar = open params[:avatar_url]
rescue
logger.warn "Unable to open #{params[:avatar_url]}"
end
end
if #user.save
# Regular response handling goes here...
Related
When integrating my RoR application with AWS to store a user's Facebook photo, the photo is stored as "original" rather than "medium".
Here is the relevant information from my User model:
class User < ActiveRecord::Base
has_attached_file :avatar,
:storage => :s3,
:style => {:medium => "370x370", :thumb => "100x100"}
validates_attachment_content_type :avatar, :content_type => /\Aimage\/.*\Z/
Calling the original photo is successful, but I need the medium thumbnail, because displaying the photo as original presents an aesthetic issue.
Here is the relevant information from my _user.html.erb user file:
<div class="img"><%= image_tag user.avatar.url(:original), height: "370", width: "370" %> </div>
however, like I said, I do not want to put original, I would like to call the file from AWS, like so:
<%= image_tag user.avatar.url(:medium)
When I inspect the page, I see that ":medium" it is not being saved, so I would assume this is a problem with the model user.rb, but any and all help is greatly appreciated, as I've been stuck on this issue for quite some time.
I assume you need to first set the validator. I think PaperClip cares about the image size. So in your model:
validates_with AttachmentSizeValidator, :attributes => :avatar, :less_than => 1.megabytes
That worked for me.
I work with Rails 4 and use PaperClip to attach images to different models.
I would like to retrieve styles dimensions in order to use them as a hint for user, something like:
<%= t('image_dimensions_hint', :dimensions => Person.avatar.standard') %>
When my model Person looks like that:
has_attached_file :avatar,
:styles => {
:standard => "150x150>",
:small => "50x50>>",
}
Is it possible?
Not sure about the question but I think you can get the dimensions with this:
:dimensions => Paperclip::Geometry.from_file(file.queued_for_write[:original].path)
Using Rails 3.2 and Paperclip 3.4.2. I have the following:
# photo.rb
has_attached_file :data,
:styles => {
:picture_lightbox => ["600x450>", :jpg],
:picture_preview => ["250x250^", :jpg],
:picture_thumb => ["76x76^", :jpg]
},
:default_url => "placeholder_:style.png"
# shop.rb
has_many :photos
# show.html.erb
<% if !shop.photos.blank? %>
<%= image_tag(shop.photos[0].data.url(:picture_thumb)) %>
<% else %>
<%= image_tag('placeholder_picture_thumb.png') %>
<% end %>
While this works, but it defeats the purpose of specifying :default_url in photo.rb, because I don't know a way to show the default image when shop.photos (which is an array of photo objects) is blank.
This is not about asset pipeline. It's about how can I detect that shop.photos is blank, then it returns the default image url, instead of explicitly specifying the default image url. What should I change?
The purpose of :default_url on Paperclip in your case is to set a default url for photo object. But you have a problem with showing a default "cover photo" of shop. This is additional logic in your code. You cannot achieve this only with Paperclip's :default_url option. If you want to take advantage of :default_url option, I will suggest you to create a method in shop.rb which looks something like this:
def cover_url
# I guess you want to use first photo based on your code
photos.first_or_initialize.data.url(:picture_thumb)
end
Then in your view you will have just <%= image_tag(shop.cover_url) %>
Actually the default URL will work when you have a relation like
class User
has_attached_file :photo
end
when user.photo is nil, default user.photo.url will return default URL.
What you have done in your case seems correct to me.
I have used the following tutorials:
http://patshaughnessy.net/2009/5/16/paperclip-sample-app-part-2-downloading-files-through-a-controller
This tutorial walks you through downloading files through the controller rather than as static files. I did this to control access to the files.
http://emersonlackey.com/article/paperclip-with-rails-3
This tutorial walks you through the process of creating a model for your files and using that to allow multiple file uploads
So, for the most part I got the uploading to work fine, however, the problem is in the downloading.
A little background. I have the following key models.
class VisualSubmission < ActiveRecord::Base
has_many :assets
accepts_nested_attributes_for :assets, :allow_destroy => true;
end
class Asset < ActiveRecord::Base
belongs_to :visual_submission
has_attached_file :image, :styles => { :original => "100%", :large => "600x600>", :small => "150x150>", :thumb => "50x50>"}, :convert_options => {:all => "-auto-orient"},
:path => ':rails_root/secure/system/:attachment/:id/:style/:basename.:extension',
:url => '/:class/:id/:attachment?style=:style'
end
In order to download files through the controller, an action is created as such:
def images
visual_submission = VisualSubmission.find(params[:id])
style = params[:style] ? params[:style] : 'original'
send_file visual_submission.image.path(style), :type => visual_submission.image_content_type, :disposition => 'inline'
end
So, as I said above, uploading is just fine. One thing that is different but expected, when it stores the file it uses the ID from the assets model. This is all well and good, however, I am having trouble getting the URL correct now. I created a route to my images:
resources :visual_submissions do
member do
get :images
end
end
And this is what my route looks like.
images_visual_submission GET /visual_submissions/:id/images(.:format) {:action=>"images", :controller=>"visual_submissions"}
Now the piece of code that in theory is supposed to access the image.
This is from my edit form. It is supposed to show the current images stored.
<%= f.fields_for :assets do |asset_fields| %>
<% unless asset_fields.object.new_record? %>
<p>
<%= asset_fields.object.image.url %>
</p>
<% end %>
<% end %>
Now this is obviously not going to work. I am not sure what object is doing here, but what I do know is, my images should be going through my visual_submissions controller which this is not touching. I am not entirely sure if I am asking this questions correctly, but I am stuck. One idea I had was to create a assets controller and move the images method into there but I am not sure how much that will help.
Any thoughts?
Thanks!
#Raymond, just put the last code snippet inside the _form.html.erb corresponding to your visual_submissions. Just be sure that you put it before the <%= f.submit %>, inside the form_for, of course. Hope that helps,
This should be a simple thing to do, but I'm running into a wall and I'm not sure how to debug this response.
In my Image model, I have:
class Image < ActiveRecord::Base
has_attached_file :image, :styles => { :display => "500x500>",
:thumbnail => "95x95>"}
Then in my Views, my form contains this:
-form_for #image, :html => { :multipart => true } do |image|
%tr
%td.woc_left
=label_tag :image, 'photo to upload', :class => 'required'
%td.woc_center
=image.file_field :image
In my Mysql table, I have a column called "image_file_name" (string).
However, when I try to upload an image and submit it, I see
2 errors prohibited this from being saved
There were problems with the following fields:
Image Paperclip::CommandNotFoundError
Image Paperclip::CommandNotFoundError
What am I doing wrong? Thank you for your help!
Paperclip relies on external tools to resize the images. It looks like you either don't have the tools installed or they are not found.
I've used ImageMagick but other tools may be usable by Paperclip.
Which environment are you running on?