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.
Using Paperclip 3.0.1 in rails 3.2.2 I got this error:
**Paperclip::AdapterRegistry::NoHandlerError**
(No handler found for "2009-11-29-133527.jpg"):
In my model I have:
class Product < ActiveRecord::Base
...
has_many :assets
accepts_nested_attributes_for :assets
end
class Asset < ActiveRecord::Base
belongs_to :product
has_attached_file :image,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename",
:styles => { :medium => "300x300>", :thumb => "100x100>" }
end
The exception is raised at:
def create
**#product = Product.new params[:product]**
...
end
with params:
{...,
"product"=>{"title"=>"wibble1",
**"assets_attributes"=>{"0"=>{"image"=>"2009-11-29-133527.jpg"}
},**
"description"=>"Who is wibble...",
"price"=>"23.45"
},
"commit"=>"Create Product",
...}
Anyone know what's going on?
This Error is raised because you aren't giving Paperclip a correct class. It's a just a String.
You should receive something like this in params
"asset"=>
{"image"=>
#<ActionDispatch::Http::UploadedFile:0x000000056679e8
#content_type="image/jpg",
#headers= "Content-Disposition: form-data; name=\"asset[image]\";
filename=\"2009-11-29-133527.jpg\"\r\nContent-Type: image/jpg\r\n",
#original_filename=""2009-11-29-133527.jpg"",
#tempfile=#<File:/tmp/RackMultipart20120619-1043-yvc9ox>>}
And you should have something like this in yout View (in HAML, very simplified):
= form_for #product, html: { multipart: true } do |f|
= f.fields_for :asset do |asset_form|
= asset_form.file_field :image
Remember to set your form to multipart: true.
I just ran into this problem myself. In my case it was caused by skipping the multipart form declaration in the markup.
I was using formtastic so I added this and got it working:
semantic_form_for #picture, :html => {:multipart => true} do |f|
Note there is a situation when working with an HTML5 canvas that is worth noting. Getting the canvas data as a DataURI string and sending that to server can cause this error. Canvas .toDataURL() will give something like "data:image/png;base64,iVBORw0KGg..." which you can send to server with other information, so its different than a standard multi-part form upload. On the server side if you just set this to the paperclip attachment field you will get this error. You need to conver it to a file or IO object. You could write a temp file like this:
data_uri = params[:canvasDataUri]
encoded_image = data_uri.split(",")[1]
decoded_image = Base64.decode64(encoded_image)
File.open("signature.png", "wb") { |f| f.write(decoded_image) }
or use Ruby's StringIO which acts like an in memory file interface
#docHolder.document = StringIO.new(decoded_image)
Hope this helps.
I had <input type="file" ... multiple="multiple"> on file input, so paperclip attachment data was in an array.
I solved this simply by removing multiple attribute on file input.
my problem was not accepting get method in routes so i changed it as patch method and it work fine.
<%= form_for #product, :url => "/products/#{#product.id}/upload",:method => :patch, :html => { :multipart => true } do |f| %>
I'm pretty sure your problem is with the form_for in the view,
try something like this:
<%= form_for #restaurante, :html => { :multipart => true } do |form| %>
Nome:<%= form.text_field :nome%>
Endereço:<%= form.text_field :endereco %>
Especialidade:<%= form.text_field :especialidade %>
Foto:<%= form.file_field :foto %>
<%= form.submit 'create'%>
<% end %>
In my case I was passing String, as in #MauricioPasquierJuan's answer, but I wasn't using a form so the rest of the answer doesn't apply.
I couldn't find any documentation of how to programatically update an attachment - what types can be assigned, and why assigning and saving the modified record does not save modified attachments. This question was the closest thing I found.
Inside a function that process files inside an uploaded zip file, after saving extracted files to temp files, this is my solution:
record.attachment = File.new( tempfile_path_as_string ) ## probably not the only option
record.attachment.save ## next line doesn't update attachment without this
record.save
Make sure you migrate the database after you install Paperclip ('rake db:migrate')... Also, you might need to add the new data fields generated by Paperclip to your 'attr_accessible' line in the model.
I had a similar problem when I was trying to get Paperclip workin on one of my projects.
I have met the same problem, I think it is because there are two tables sharing the same attached_file_name... In my case, I add a :photo column both to activities and tweets, then the case seems to be that the system can find one of them but not the other. Because the files are saved in /public/photo/:id/:id path, if you have two columns both named as photo, then the problem occurs I think.
for me the problem was like this:
I used such line in controller, as saw it in some answers:
#image = User.find(params[:id]).image.<b>path</b>(:small)
and I had the problem "no handler for the file"
so, I just removed "path" and it worked:
#image = User.find(params[:id]).image(:small)
When we upgraded from 4.2.x to 4.3.x, we had to change the main paperclip field attribute (picture, image, etc) from a relative URL to a full URL to eliminate this error.
In my application I have the option for a user to upload an image for a profile picture through paperclip.
Want I want to do in the index.html.erb page is to display all users with their display pictures, and if they have not uploaded one then it should display a default picture.
Now I have tried coding it in but at the moment the users who have pictures have the displayed but those who don't get nothing.
I have the following code in my user model:
def user_photo
(photo.url.present?) ? photo.url : '/images/guest.png'
end
and the following code in my index.html.erb
<% for user in #users %>
.............
<%= image_tag user.user_photo, :height => 50, :width => 50 %>
..............
<% end %>
Any ideas on what my problem might be?
Thanks
Have something like this in your user model:
has_attached_file :photo,
:default_url => '/images/guest.jpeg',
:styles => {
:large => "300x300>",
:thumb => "100x100>",
:tiny => "32x32>"
}
The :default_url option is used if there is no attached file for a model. If a user doesn’t have any uploaded photo you could use this option to set a default photo to show.
In the view:
<%= image_tag #user.photo.url(:thumb) %>
Read Paperclip usage guide
Since this is view logic, I would put it in a helper instead of in the model. So in a relevant helper:
def user_photo(user)
user.photo.url.present? ? image_tag user.user_photo : image_tag 'guest.png'
end
Then call that helper from the view. Of course you can add other parameters to image_tag for the dimensions and so on. This way you get the benefit of using the image_tag helpers consistently.
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.
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?