Paperclip wrong attachment url on validation errors - ruby-on-rails

I have a preview of attached image in my update form. The problem appears when user gets validation errors on attachment field. In this case image thumbnail url becomes as if an image was uploaded without any errors (it shows name of file that was not saved at server).
Here is how I get image url in my view:
<%= image_tag(#product.photo.url(:medium)) %>.
Controller:
def update
#product = Product.find(params[:id])
#product.update_attributes(params[:product]) ? redirect_to('/admin') : render(:new)
end
def edit
#product = Product.find(params[:id])
render :new
end
Model:
class Product < ActiveRecord::Base
<...>
##image_sizes = {:big => '500x500>', :medium => '200x200>', :thumb=> '100x100>'}
has_attached_file :photo, :styles => ##image_sizes, :whiny => false
validates_attachment_presence :photo
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/jpg', 'image/png', 'image/gif'], :message => I18n.t(:invalid_image_type)
validates_attachment_size :photo, :less_than => 1.megabytes, :message => I18n.t(:invalid_image_size, :max => '1 Mb')
after_post_process :save_image_dimensions
<...>
end
UPD: The simpliest solution is to add #photo_file_url = #product.photo.url(:medium) in controller's update action before #product.update_attributes and <% #photo_file_url ||= #product.photo.url(:medium) %> in the view.

I actually had the same issue and here is what I had done (which seems a bit of a hack) but works and will show the default or previous image on update failure
after_validation :logo_reverted?
def logo_reverted?
unless self.errors[:logo_file_size].blank? or self.errors[:logo_content_type].blank?
self.logo.instance_write(:file_name, self.logo_file_name_was)
self.logo.instance_write(:file_size, self.logo_file_size_was)
self.logo.instance_write(:content_type, self.logo_content_type_was)
end
end

In my app I had similar issues, so I used:
<%= image_tag(#product.photo.url(:medium)) if #photo.valid? %>
That way nothing shows up if the Photo isn't valid (ie successfully created), but when you edit existing records the image is shown.
Hope that helps.

Related

Getting error when validating picture presence Rails 4

This is my Gig model
class Gig < ActiveRecord::Base
has_attached_file :image, :styles => { :medium => "360x170>", :bigger => "650x459>" }
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
validate :image_size_validation
def image_size_validation
if image.size > 2.megabytes
errors.add(:base, "Image should be less than 2MB")
end
end
end
Everything works great,i can not upload a picture greater than 2Mb as i wanted,and it gives a notice about that(written by me).
Question: When I save the form(of a new product),without selecting any picture,it throws an error saying undefined method >
for nil:NilClass. Instead of just saying to the user,that he should
upload a picture,so the form can be saved.
Gig controller for create is this
def create
#gig = current_user.gigs.build(gig_params)
if #gig.save
redirect_to #gig, notice: "Gig successfully created"
else
render "new"
end
end
That error is clearly thrown by this line:
if image.size > 2.megabytes
obviously, if there isn't an image... it won't have a size.
How about you check for nil there:
def image_size_validation
return if image.blank?
if image.size > 2.megabytes

Cropping using Paperclip, ImageMagick, Jcrop and S3 storage: Why won't 'image.reprocess!' reprocess?

I'm (loosely) following RailsCasts tutorial #182, which uses Paperclip, ImageMagick, and Jcrop to allow custom cropping of uploaded images.
As I'm using Amazon S3 for file storage, I've had to rejigger parts of the tutorial to suit. Everything seems to be working perfectly except for the fact that the cropped version of my image isn't being reprocessed (or the result of that reprocess is not re-uploaded to S3)--so after the cropping process, I'm left with the same image I originally uploaded (this is true for all image sizes I store for each image).
Here is my Feature (as in Feature Image) model:
class Feature < ActiveRecord::Base
require "#{Rails.root}/lib/paperclip_processors/cropper.rb"
attr_accessible :image_file_name, :image
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
after_update :reprocess_image, :if => :cropping?
if Rails.env == "production"
S3_CREDENTIALS = { :access_key_id => '<REDACTED>',
:secret_access_key => '<REDACTED>',
:bucket => "<REDACTED>"}
else
S3_CREDENTIALS = { :access_key_id => '<REDACTED>',
:secret_access_key => '<REDACTED>',
:bucket => "<REDACTED>"}
end
has_attached_file :image,
:styles => { :small => "240x135>", :croppable => "960x960>", :display => "960x540>" },
:processors => [:cropper],
:storage => :s3,
:s3_credentials => S3_CREDENTIALS,
:path => "features/:id/:style.:extension"
validates_attachment_content_type :image, :content_type => ['image/jpeg', 'image/gif', 'image/png',
'image/pjpeg', 'image/x-png'],
:message => 'must be a JPEG, GIF or PNG image'
def cropping?
!crop_x.blank? && !crop_y.blank? && !crop_w.blank? && !crop_h.blank?
end
def image_geometry(style = :original)
#geometry ||= {}
path = (image.options[:storage]==:s3) ? image.url(style) : image.path(style)
#geometry[style] ||= Paperclip::Geometry.from_file(path)
end
private
def reprocess_image
image.reprocess!
end
end
Here is my 'cropper.rb' (Paperclip processor):
module Paperclip
class Cropper < Thumbnail
def transformation_command
if crop_command
crop_command + super.sub(/ -crop \S+/, '')
else
super
end
end
def crop_command
target = #attachment.instance
if target.cropping?
" -crop '#{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}'"
end
end
end
end
The relevant actions of my FeaturesController:
class FeaturesController < ApplicationController
def new
#feature = Feature.new
end
def create
#feature = Feature.new(params[:feature])
if #feature.save
if params[:feature][:image].blank?
flash[:notice] = "New feature added!"
redirect_to #feature
else
render :crop
end
else
#title = "Add a New Feature"
render :new
end
end
def edit
#feature = Feature.find(params[:id])
#title = "Edit #{#feature.headline}"
end
def update
#feature = Feature.find(params[:id])
if #feature.update_attributes(params[:feature])
if params[:feature][:image].blank?
flash[:notice] = "Feature updated!"
redirect_to #feature
else
render :crop
end
else
#title = "Edit Feature"
render :edit
end
end
end
And the relevant lines of my 'crop.html.erb' view:
<% content_for :javascript_includes do %>
<%= javascript_include_tag 'jquery.Jcrop.min' %>
<script type="text/javascript" charset="utf-8">
$(function() {
$('#cropbox').Jcrop({
onChange: update_crop,
onSelect: update_crop,
setSelect: [0, 0, 960, 540],
aspectRatio: 960/540
});
});
function update_crop(coords) {
var ratio = <%= #feature.image_geometry(:original).width %> / <%= #feature.image_geometry(:croppable).width %>;
$("#crop_x").val(Math.round(coords.x * ratio));
$("#crop_y").val(Math.round(coords.y * ratio));
$("#crop_w").val(Math.round(coords.w * ratio));
$("#crop_h").val(Math.round(coords.h * ratio));
};
</script>
<% end %>
<% content_for :style_includes do %>
<%= stylesheet_link_tag 'jquery.Jcrop', :media => 'screen' %>
<% end %>
<%= image_tag #feature.image.url(:croppable), :id => "cropbox" %>
<% form_for #feature do |f| %>
<% for attribute in [:crop_x, :crop_y, :crop_w, :crop_h] %>
<%= f.hidden_field attribute, :id => attribute %>
<% end %>
<p><%= f.submit "Crop" %></p>
<% end %>
The problem is not that there's an error with the custom crop (offset, crop area, etc.), it's that there is no crop happening when I click 'crop'--I'm just left with the images I got from the original upload/process. It doesn't appear that 'image.reprocess!' is happening at all (or the results of the reprocess aren't being saved to S3).
Why might that be, and what can I do about it?
Don't know if this is the same but I had a problem with the reprocessing and fixed it by following the answer here:
paperclip error while reporcessing after rails 3 upgrade
Ok, let me try to help :)
Firstly, can you not include your Paperclip processor in the model, let Paperclip handle it.
Secondly, remove the :after_update and replace with a :before_update which should set your image.options[ :crop ] up for the processor. In your processor, try this:
def initialize file, options = {}, attachment = nil
super
#......
#crop = options[ :crop ]
#......
Thirdly, modify your transformation_command in the processor:
def transformation_command
if #crop
trans = " -quality 75"
trans << " -crop \"#{<YOUR_CODE>}\" +repage" if #crop
trans
end
end
And then post your findings :)
I also used that same RailsCast video to help me get cropping to work. I was running into a similar issue with a NOSUCHKEY error which I was able to trace to the reprocessing! call that was being called after_update. I was able to fix the problem by using Vish's answer and modifying it. My solution may not be exactly what you need as I crop every time.
I simply removed the after_update call to reprocess! and left everything else in place. This caused the passed in processor (cropper in your case) to be used instead of the default and occurs before uploading the image to S3. Since you crop the file before uploading it for the first time everything works!
If you wanted to have conditional cropping then you would want to do what Vish suggested which is to set a variable or pass an option with the image object in a before_update. It needs to be available in your custom processor so you can put your conditional cropping logic in place based on its value.

Rails 3 Paperclip Uploadify : Save Uploaded Object Attachments Before Saving Object

I am working with Rails 3 and Paperclip to attach uploaded files to several object types using a polymorphic association. I have created an Asset model and an inherited Image model (will be adding others, like Video and Documents later) as follows:
# app/models/asset.rb
class Asset < ActiveRecord::Base
# Nothing here yet
end
# app/models/image.rb
class Image < Asset
belongs_to :assetable, :polymorphic => true
has_attached_file :file, {
:styles => {
:small => { :geometry => '23x23#', :format => 'png' },
:medium => { :geometry => '100x100#', :format => 'png' } }
}.merge(PAPERCLIP_STORAGE_OPTIONS).merge(PAPERCLIP_STORAGE_OPTIONS_ASSET_IMAGE) # Variables sent in environments to direct uploads to filesystem storage in development.rb and S3 in production.rb
validates_attachment_presence :file
validates_attachment_size :file, :less_than => 5.megabytes
end
I then have another object type, Unit, which I am attaching multiple images to as follows:
# app/models/unit.rb
class Unit < ActiveRecord::Base
# ...
has_many :images, :as => :assetable, :dependent => :destroy
accepts_nested_attributes_for :images
end
# app/controllers/units_controller.rb
class UnitsController < ApplicationController
# ...
def new
#unit = current_user.units.new
# ...
#unit.images.build
end
def create
#unit = current_user.units.new(params[:unit])
# ...
respond_to do |format|
if #unit.save
format.html { redirect_to(#unit, :notice => 'Unit creation successful!') }
else
format.html { render :action => "new" }
end
end
end
def show
#unit = current_user.units.find(params[:id])
#unit_images = #unit.images
# ...
end
def edit
#unit = current_user.units.find(params[:id])
# ...
#unit.images.build
end
def update
#unit = current_user.units.find(params[:id], :readonly => false)
respond_to do |format|
if #unit.update_attributes(params[:unit])
format.html { redirect_to(#unit, :notice => 'Unit was successfully updated.') }
else
format.html { render :action => "edit" }
end
end
end
def destroy
#unit = current_user.units.find(params[:id])
#unit.destroy
respond_to do |format|
format.html { redirect_to(units_url) }
end
end
end
# app/views/units/_form.html.haml
.field # Display already uploaded images
= f.fields_for :images do |assets|
- unless assets.object.new_record?
= link_to(image_tag(assets.object.file.url(:medium)), assets.object.file.url(:original))
.field # Display field to add new image
= f.fields_for :images do |assets|
- if assets.object.new_record?
= assets.label :images, "Image File"
= assets.file_field :file, :class => 'uploadify'
Using these settings I am able to upload images one at at time, per display of the form.
The issues start when I try to integrate Uploadify to add multi file uploading/previewing. I have satisfied all the Uploadify dependancies, but in order to save the images associated with the Unit model I need to somehow include a reverence to the unit_id so that the polymorphic association can be made properly. Below is my current Uploadify code:
%script
$(document).ready(function() {
$('.uploadify').uploadify({
uploader : '/uploadify/uploadify.swf',
cancelImg : '/uploadify/cancel.png',
auto : true,
multi : true,
script : '#{units_path}',
scriptData : {
"#{key = Rails.application.config.session_options[:key]}" : "#{cookies[key]}",
"#{request_forgery_protection_token}" : "#{form_authenticity_token}",
}
});
});
So while I can easily upload with Paperclip along, Uploadify will not work. Any help would be much appreciated. Thank you in advance.
UPDATE:
After doing more research I ran across this comment to a similar issue: Rails3, S3, Paperclip Attachment as it's own model?. Any thoughts on whether or not that would work in this situation? Is there an easy way of determining the unit.id from the /new method and passing it to the Uploadify-created Asset?
We had solved a very similar problem once by saving the model right away when loading the form in a draft state (using state machine). Like this the model is available when you're trying to attach the files you're uploading and once you're submitting the rest of the form, you're basically just updating the model which changes it's state to e.g. published. It's a little work to update the controllers etc., but it did the trick.

Uploading to S3 on Heroku with Paperclip (delayed_job question)

I'm trying to upload to a portfolio app I've built, specifically trying to find where to hook delayed_job into the process. It all works otherwise. Right now it returns undefined method 'call' for #<Class:0xae68750> on app/controllers/portfolio_items_controller.rb:18:in 'create' so here's my model and that portion of the controller... anyone see anything that could be going wrong? The hook I'm using now I got from this blog: http://madeofcode.com/posts/42-paperclip-s3-delayed-job-in-rails
/app/controllers/portfolio_items_controller.rb
def create
#portfolio_item = PortfolioItem.new(params[:portfolio_item])
if #portfolio_item.save
flash[:notice] = "Portfolio item created. As soon as files are uploaded Portfolio item will be made live."
redirect_to #portfolio_item
else
render :action => 'new'
end
end
/app/models/asset.rb
class Asset < ActiveRecord::Base
attr_accessible :image, :image_file_name, :image_content_type, :image_file_size, :portfolio_item_id, :order
belongs_to :portfolio_item
has_attached_file :image,
:styles => {
:thumb => "20x20#",
:small => "100x100",
:large => "600x600>"
},
:storage => :s3,
:s3_credentials => {
:access_key_id => ENV["S3_KEY"],
:secret_access_key => ENV["S3_SECRET"]
},
:bucket => ENV["S3_BUCKET"],
:path => "portfolio/:attachment/:id/:style/:basename.:extension"
before_source_post_process do |image|
if source_changed?
processing = true
false
end
end
after_save do |image|
if image.source_changed?
Delayed::Job.enqueue ImageJob.new(image.id)
end
end
def regenerate_styles!
self.source.reprocess!
self.processing = false
self.save(false)
end
def source_changed?
self.source_file_size_changed? ||
self.source_file_name_changed? ||
self.source_content_type_changed? ||
self.source_update_at_changed?
end
end
class ImageJob < Struct.new(:image_id)
def perform
Image.find(self.image_id).regenerate_styles!
end
end
Edit: thanks to kind people, it's not the missing .new anymore. But now it's that the before_source_post_process is not defined? And I can't find that method in anywhere but that blog post and this SO question. Is there something more appropriate?
The before_source_post_process won't work for you. It only works for:
has_attached_file :source
In your case it should be
before_image_post_process
Similarly, the source_changed? method should be:
def source_changed?
self.image_file_size_changed? ||
self.image_file_name_changed? ||
self.image_content_type_changed? ||
self.image_update_at_changed?
end
I think this:
#portfolio_item = PortfolioItem.(params[:portfolio_item])
should most likely be this:
#portfolio_item = PortfolioItem.new(params[:portfolio_item])

Saving files using Paperclip without upload

I had a quick question. Is it possible to save a file without actually uploading it through a form?
For example, let's say I'm looking at attachments from emails, and I want to save them using a paperclip. How do I do this? Do I manually have to call a save_file(or something similar) somewhere?
Any help would be much appreciated!
I have a rake task that loads images (client logos) from a directory directly onto parperclip. You can probably adapt it to your needs.
This is my simplified Client model:
class Client < ActiveRecord::Base
LOGO_STYLES = {
:original => ['1024x768>', :jpg],
:medium => ['256x192#', :jpg],
:small => ['128x96#', :jpg]
}
has_attached_file :logo,
:styles => Client::LOGO_STYLES,
:url => "/clients/logo/:id.jpg?style=:style"
attr_protected :logo_file_name, :logo_content_type, :logo_size
Then on my rake task I do this:
# the logos are in a folder with path logos_dir
Dir.glob(File.join(logos_dir,'*')).each do |logo_path|
if File.basename(logo_path)[0]!= '.' and !File.directory? logo_path
client_code = File.basename(logo_path, '.*') #filename without extension
client = Client.find_by_code(client_code) #you could use the ids, too
raise "could not find client for client_code #{client_code}" if client.nil?
File.open(logo_path) do |f|
client.logo = f # just assign the logo attribute to a file
client.save
end #file gets closed automatically here
end
end
Regards!
The file saved in Paperclip doesn't have to be uploaded directly through a form.
I'm using Paperclip in a project to save files from URLs from webcrawler results. I'm not sure how you'd get email attachments (are they on the local file system of the server? Is your app an email app like GMail?) but as long as you can get a file stream (via something like open(URI.parse(crawl_result)) in my case...) you can attach that file to your model field that's marked has_attached_file.
This blog post about Easy Upload via URL with Paperclip helped me figure this out.
Since it now appears the original blog post is no longer available - here's the gist of it pulled from wayback machine:
This example shows a Photo model that has an Image attachment.
The technique we're using requires adding a *_remote_url (string) column for your attachment, which is used to store the original URL. So, in this case, we need to add a column named image_remote_url the photos table.
# db/migrate/20081210200032_add_image_remote_url_to_photos.rb
class AddImageRemoteUrlToPhotos < ActiveRecord::Migration
def self.up
add_column :photos, :image_remote_url, :string
end
def self.down
remove_column :photos, :image_remote_url
end
end
Nothing special is required for the controller...
# app/controllers/photos_controller.rb
class PhotosController < ApplicationController
def create
#photo = Photo.new(params[:photo])
if #photo.save
redirect_to photos_path
else
render :action => 'new'
end
end
end
In the form, we add a text_field called :image_url, so people can upload a file or provide a URL...
# app/views/photos/new.html.erb
<%= error_messages_for :photo %>
<% form_for :photo, :html => { :multipart => true } do |f| %>
Upload a photo: <%= f.file_field :image %><br>
...or provide a URL: <%= f.text_field :image_url %><br>
<%= f.submit 'Submit' %>
<% end %>
The meaty stuff is in the Photo model. We need to require open-uri, add an attr_accessor :image_url, and do the normal has_attached_file stuff. Then, we add a before_validation callback to download the file in the image_url attribute (if provided) and save the original URL as image_remote_url. Finally, we do a validates_presence_of :image_remote_url, which allows us to rescue from the many exceptions that can be raised when attempting to download the file.
# app/models/photo.rb
require 'open-uri'
class Photo < ActiveRecord::Base
attr_accessor :image_url
has_attached_file :image # etc...
before_validation :download_remote_image, :if => :image_url_provided?
validates_presence_of :image_remote_url, :if => :image_url_provided?, :message => 'is invalid or inaccessible'
private
def image_url_provided?
!self.image_url.blank?
end
def download_remote_image
self.image = do_download_remote_image
self.image_remote_url = image_url
end
def do_download_remote_image
io = open(URI.parse(image_url))
def io.original_filename; base_uri.path.split('/').last; end
io.original_filename.blank? ? nil : io
rescue # catch url errors with validations instead of exceptions (Errno::ENOENT, OpenURI::HTTPError, etc...)
end
end
Everything will work as normal, including the creation of thumbnails, etc. Plus, since we're doing all of the hard stuff in the model, "uploading" a file via URL works from within script/console as well:
$ script/console
Loading development environment (Rails 2.2.2)
>> Photo.new(:image_url => 'http://www.google.com/intl/en_ALL/images/logo.gif')
=> #<Photo image_file_name: "logo.gif", image_remote_url: "http://www.google.com/intl/en_ALL/images/logo.gif">

Resources