Rails3 and Paperclip

本秂侑毒 提交于 2019-12-20 15:08:10

问题


I have migrated my application from rails 2.3 to rails3 and i have a problem with paperclip. I saw there was a branch for rails3 on paperclip git.

So I added "gem 'paperclip', :git => 'git://github.com/thoughtbot/paperclip.git', :branch => 'rails3'" into the Gemfile and launch the command bundle install.

Once paperclip installed, the upload worked fine but not the styles. I saw a hack to fix it.

# in lib/paperclip/attachment.rb at line 293
def callback which #:nodoc:
  # replace this line...
  # instance.run_callbacks(which, @queued_for_write){|result,obj| result == false }
  # with this:
  instance.run_callbacks(which, @queued_for_write)
end

The styles are ok after that, but i'm not able to active the processor. My code is :

has_attached_file                 :image,
                                  :default_url => "/images/nopicture.jpg",
                                  :styles => { :large   => "800x600>",
                                               :cropped => Proc.new { |instance| "#{instance.width}x#{instance.height}>" },
                                                :crop    => "300x300>" },
                                   :processors => [:cropper]

My processor is located in RAILS_APP/lib/paperclip_processors/cropper.rb and contains :

module Paperclip
  class Cropper < Thumbnail
    def transformation_command
      if crop_command  and !skip_crop?
        crop_command + super.sub(/ -crop \S+/, '')
       else
         super
       end
    end

   def crop_command
      target = @attachment.instance
      trans = "";
      trans << " -crop #{target.crop_w}x#{target.crop_h}+#{target.crop_x}+#{target.crop_y}" if target.cropping?
      trans << " -resize \"#{target.width}x#{target.height}\""
      trans
   end

   def skip_crop?
     ["800x600>", "300x300>"].include?(@target_geometry.to_s)
   end
 end
end

My problem is that i got this error message : uninitialized constant Paperclip::Cropper The cropped processor is not loaded.

Is anybody has an idea to fix that ?

For information my application works fine on rails 2.3.4.


回答1:


I have the same problem. Seems like paperclip processors are not loaded in rails 3. Until someone fix it, I hacked the problem moving the cropper.rb file inside /config/initializers




回答2:


Restart server, worked for me :)




回答3:


I got "uninitialized constant Paperclip::Cropper"exception too. But you can move your cropper.rb to the paperclip lib directory and add require 'paperclip/cropper.rb'. It work for me, but I use bundler, so it seems very ugly...




回答4:


In your Photo.rb class, or whichever one you used to have require '.../cropper.rb' you have to put the root of your project in the path as well:

require "#{config.root}/lib/paperclip_processors/cropper.rb" # required to make cropping work.

before it used to just be require "lib/..."




回答5:


Yep, install Paperclip as a plugin and it will work




回答6:


I recommend you carrierwave instead paperclip, is much fast and easier https://github.com/jnicklas/carrierwave




回答7:


I have the same problem. I'm using the paperclip gem from this repository:

gem "paperclip", :git => "git://github.com/JasonKing/paperclip.git", :branch => "rails3"

And I also get "uninitialized constant Paperclip::Cropper". Restarting the server didn't work!



来源:https://stackoverflow.com/questions/2341216/rails3-and-paperclip

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!