I use Rails 5.1.4 and paperclip. I have i18n website http://example.com/en, http://example.com/de, http://example.com/uk.
How to serve file with different source (img src=?)file name depend on locale?
http://example.com/en/house.jpg
http://example.com/de/haus.jpg
http://example.com/uk/komnata.jpg
Is it possible with paperclip?
How to do it?
Regards
Ssebaaa
You're looking for paperclip interpolations
https://github.com/thoughtbot/paperclip/wiki/Interpolations
This is how you would use them.
In your model that utilizes paperclip, update the path to something of your choice:
has_attached_file :file,
.......
:path => ':locale/:style/:filename'
Then tell paperclip to look for the keyword, and what to replace it with
Paperclip.interpolates :locale do |attachment, style|
attachment.some_logic_on_knowing_what_locale
end
(this part goes in paperclip.rb initializer)
Related
I'm using rails 4.2 and paperclip 4.2.1. I have several rails model with paperclip attachments so I set default config for paperclip in development.rb like this:
config.paperclip_defaults = config_for(:paperclip_settings)
and set paperclip_settings.yml to:
development:
:storage: :s3
:s3_credentials:
:bucket: "xxx"
:access_key_id: "yyy"
:secret_access_key: "zzz"
:url: ':s3_path_url'
:path: '/:attachment/:id_partition/:style/:filename
and I've added one more attachment only for one model that must saves locally
has_attached_file :faq, :storage => :filesystem but it still tries to save this attachment to S3. So the question is how can I separate config for S3 and filesystem uploading and call them on paperclip has_attached_file or just fill all needed properties to this attachment.
Thanks!
I can no longer render pages with paperclip urls in my Rails application. I recently updated my gem bundle. I'm using Rails 3.2.8 and Paperclip 3.1.4. I was using Paperclip 2.7.0 before.
The link in my view that is failing is:
ad.image.url(:medium)
My Ad model has these declarations:
Paperclip.interpolates :ad_subdomain do |attachment, style|
attachment.instance.brand.subdomain
end
has_attached_file :image,
:default_url => '/images/blank.gif',
:styles => { :medium => ["290x230>","jpg"],
:thumb => ["100x100>","jpg"] },
:storage => :file,
:path => "/mcp/ads/:style/:ad_subdomain/:basename.:extension"
The error that is being thrown is:
TypeError: wrong argument type Class (expected Module)
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:368:in `extend'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:368:in `initialize_storage'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/attachment.rb:80:in `initialize'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/instance_methods.rb:5:in `new'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip/instance_methods.rb:5:in `attachment_for'
from /Users/me/.rvm/gems/ruby-1.9.3-p0#mcp5/gems/paperclip-3.1.4/lib/paperclip.rb:191:in `block in has_attached_file'
For some reason, I had:
:storage => :file
in my declaration. That does not work. If you want to use the filesystem, you just leave out the storage line altogether and it will default.
Check out the requirements listed on the Paperclip github page:
Paperclip now requires Ruby version >= 1.9.2 and Rails version >= 3.0
(Only if you're going to use Paperclip with Ruby on Rails.)
If you're still on Ruby 1.8.7 or Ruby on Rails 2.3.x, you can still
use Paperclip 2.7.x with your project. Also, everything in this README
might not apply to your version of Paperclip, and you should read the
README for version 2.7 instead.
Why am I getting no such file or directory when I use a symbol in my path? Paperclip shows :rails_root/public/system/:class/:attachment/:id_partition/:style/:filename as the default path. The file is in public/system/attachments/email.txt, but I want to be able to add a file with any name.
has_attached_file :email,
:url => "public/system/attachments/:filename",
:path => "public/system/attachments/:filename"
def read_original_header
#original_email = File.read("public/system/attachments/:filename")
end
No such file or directory - public/system/attachments/:filename
Your issue is that the 'symbol' interpolation is a Paperclip convention, this convention is not shared by the standard Ruby File class.
I'm new in Ruby/Rails, so, my question: what is the perfect way to upload file and save filename in database with rails? Are there any Gem? Or maybe there are good build-in feature?
Also try carrierwave, Ryan has a good screencast about this gem
Just take a look on the links to choose between paperClip & carrierwave :
Rails 3 paperclip vs carrierwave vs dragonfly vs attachment_fu
And
http://bcjordan.github.com/posts/rails-image-uploading-framework-comparison/
You can try Paperclip.It is most popular gem in rails community...
https://github.com/thoughtbot/paperclip
these lines do the all stuff
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
end
Try it......
Most people use the Paperclip Gem.
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.
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
Restart server, worked for me :)
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...
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/..."
Yep, install Paperclip as a plugin and it will work
I recommend you carrierwave instead paperclip, is much fast and easier https://github.com/jnicklas/carrierwave
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!