Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

attachment_update_at not updated #27

Open
pmontrasio opened this issue Aug 24, 2014 · 0 comments
Open

attachment_update_at not updated #27

pmontrasio opened this issue Aug 24, 2014 · 0 comments

Comments

@pmontrasio
Copy link

I'm not sure that this is a papercrop issue, it might be a paperclip one but I start from here because I suspect that if it were a paperclip issue it would have surfaced a long time ago. I'm using paperclip 4.2.0 and papercrop 0.2.0.

I have a model user.rb with

class User < ActiveRecord::Base

  extend FriendlyId
  friendly_id :slug_candidates, use: [ :slugged, :finders ]
  def slug_candidates
     ...
  end

  Paperclip.interpolates :slug do |attachment, style|
    attachment.instance.slug
  end

  has_attached_file :picture,
    styles: { large: "512x512!", medium: "256x256!", thumb: "128x128!", status: "32x32!" },
    default_url: "/images/:class/:style/missing.png",
    hash_secret: "whatever-it-is",
    url: "/system/:class/:attachment/:slug/:style/:hash.:extension",
    path: ":rails_root/public/system/:class/:attachment/:slug/:style/:hash.:extension"
  crop_attached_file :picture

plus some devise declarations and some associations which probably don't matter.
Note that I'm using :hash in the path. This is important.

I'm displaying the cropper with the default form code from the README

      <%= form_for @user do |f| %>
    <%= f.cropbox :picture %>
    <%= f.crop_preview :picture %>
    <%= f.submit 'Save' %>
      <% end %>

It updates the images but when I reload the page with the form I get Paperclip::Errors::NotIdentifiedByImageMagickError in the f.cropbox line.

The stack trace is

paperclip (4.2.0) lib/paperclip/geometry_detector_factory.rb:10:in `make'
paperclip (4.2.0) lib/paperclip/geometry.rb:26:in `from_file'
papercrop (0.2.0) lib/papercrop/model_extension.rb:71:in `image_geometry'
papercrop (0.2.0) lib/papercrop/helpers.rb:41:in `cropbox'
app/views/users/show.html.erb:9:in `block in _app_views_users_show_html_erb___579696657148981651_70039981047720'

(Yes, I put the form in show view but I'm only testing the gem)

The reason for that ImageMagick error is that the file name passed to the identify command doesn't exist.

After some debugging I realized that the problem is due to either papercrop or paperclip did not update the picture_updated_at field in the database. In turn that makes paperclip generate the wrong hash for the path and the url of the updated picture. Hence the error.

I worked around this issue by manually saving the attribute in the controller

if @user.update_attributes(user_params)
  @user.update_column(:picture_updated_at, @user.picture_updated_at)
  redirect_to users_path, alert: "User updated"
else
  render :edit
end

Note that @user.picture_updated_at has the correct timestamp. It doesn't make its way to the database.

Should I open this issue on the paperclip repository or is this something worth investigating in papercrop?

By the way, I found something similar at http://stackoverflow.com/questions/20429449/paperclip-attachment-not-using-proper-updated-at-time-for-query-string solved with a similar workaround. However that was with paperclip 2.7

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant