I would be really grateful if someone could help me with this.
I am using paperclip to upload images.
I modified my paperclip.rb to add the following interpolation code:
Paperclip.interpolates :submission_id do |attachment, style|
attachment.instance.submission_id
end
I have the following code included in the image.rb:
has_attached_file :data, :path => ":rails_root/public/system/datas/:submission_id/:id/:style",
:url => "/system/datas/:submission_id/:id/:style",
:styles => {
:thumb => "50x50#",
:large => "640x480#"
}
Currently, when i upload images, they are stored in the following folder structure:
submission_id/image_primary_id/image -----> 13/244/original
I would like to store the image in the following format:
SUB_submission_id/originals/imagename.jpeg ---> SUB_13/originals/image01.jpeg
Please could someone shed some light on this how to do this.
Thanks a lot for your help
Did you try something like this?
Paperclip.interpolates :submission_id do |attachment, style|
"SUB_#{attachment.instance.submission_id}"
end
And the drop the :id from the path and url (make sure you don't upload files with the same name though)
has_attached_file :data, :path => ":rails_root/public/system/datas/:submission_id/:style",
:url => "/system/datas/:submission_id/:style",
Related
I am working on an ecommerce website using Solidus, Rails. The site allows you to order photo frames & prints from a variety of options.
To print a photo a user must upload the jpg file of the photo. So, to allow that I modified the orders table and added a paperclip attachment called 'attachment'
I ran the following command
rails generate paperclip SpreeOrder attachment
Which generated the migrations, then I ran rake db:migrate
Then I created a spree/order_decorator.rb file, and added has_attached_file
module Spree::OrderDecorator
has_attached_file :attachment, styles: {
:medium => {
:geometry => "640x480",
:format => 'jpeg'
},
:thumb => { :geometry => "160x120", :format => 'jpeg', :time => 10}
}, :processors => [:transcoder]
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\z/
Spree::Order.prepend self
end
After this I ran the server, and ended up getting this error
undefined method `has_attached_file' for Spree::OrderDecorator:Module (NoMethodError)
I have configured solidus for use with paperclip only, so I am really confused as to why I am getting this error, even later I manually went and generated a paperclip.rb file in the config/initializers directory, but still I get the same error.
Please help with this!!
Thank You!!
You should add those paperclip method at class level in the prepended module:
def self.prepended(base)
base.has_attached_file
end
I am trying to make work Paperclip interpolations the whole afternoon, but still not success.
Here is how I have set up the Image model:
has_attached_file :image,
:styles => { :thumb => '300x300#',
:medium => "300x300>",
:original => "900x900>" },
:path => ":rails_root/public/images/:user_id/:style/:basename.:extension",
:url => "/images/:user_id/:style/:basename.:extension"
In /config/initializers/paperclip.rb is following:
Paperclip.options[:command_path] = "/usr/local/bin"
module Paperclip
module Interpolations
def user_id attachment, style_name
attachment.instance.user_id.to_s
end
end
end
But every time I save a file, the files is saved as
/images//original/file-name.jpg
The user's ID is missing.
What is wrong in this sample? I still cannot find the right config of Paperclip setup. I would be very grateful for every help.
Thank you
Try to add this in your Image model
Paperclip.interpolates :user_id do |attachment, style|
attachment.instance.user_id.to_s
end
I got the following message after upgrading:
Paperclip 3.0 introduces a non-backward compatible change in your
attachment path. This will help to prevent attachment name clashes
when you have multiple attachments with the same name. If you didn't
alter your attachment's path and are using Paperclip's default, you'll
have to add :path and :url to your has_attached_file definition.
For example:
has_attached_file :avatar,
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
So I did so:
post.rb:
has_attached_file :image, :styles => { :medium => "170x300>",
:thumb => "142x185>" },
:path => ":rails_root/public/system/:attachment/:id/:style/:filename",
:url => "/system/:attachment/:id/:style/:filename"
But then I saw this error message:
Image Paperclip::Errors::NotIdentifiedByImageMagickError
I even added this to environments/development.rb:
Paperclip.options[:command_path] = "/usr/bin/"
(which identify outputs /usr/bin/identify)
But still no luck.
What could be the problem?
Wow, I didn't expect this. The problem wasn't due to upgrading.
It was because the file I was uploading was named like this:
Screenshot at 2012-11-26 16:22:44.png
Weird.
The issue is in the filename.
colons are not accepted, if you remove the colon from the attachment name using gsub it'll be accepted always.
My paperclip path in the model is:
has_attached_file :image, :path => ":rails_root/app/assets/images/article_images/:id/:style_:basename.:extension"
But when I call article.image.url, here is the url I get (which is broken):
/system/images/64294/original/fantastik.jpg?1324288670
You also need to setup the URL, what you want is:
has_attached_file :image,
:path => ":rails_root/app/assets/images/article_images/:id/:style_:basename.:extension"
:url => "/app/assets/images/article_images/:id/:style_:basename.:extension"
The only thing I would question is if you're storing them in a good place. Typically, they would go in
/public/...
Or on another service like s3. However, that is how you modify the URL
Hope this helps!
like #andrewpthorp mention that's because you switch only path, but you can do it more DRY
paperclip defaults:
:url => "/system/:attachment/:id/:style/:filename",
:path => ":rails_root/public:url",
You can see that url can be part of path so your config should look like:
has_attached_file :image,
:url => "/app/assets/images/article_images/:id/:style_:basename.:extension",
:path => ":rails_root:url"
Be carefull, usualy servers (apache, nginx) serves files only from public directory.
More options for has_attached_file you can find here
This is how to fix the issue with :default_url:
:default_url => ActionController::Base.helpers.asset_path('empty-event-cover.png')
How to display pdf file which is stored in s3 amazon in rails application???
when uploading files to S3 the filename has to have No Spaces or special caracters.
To upload files with spaces use the following
yourmodel.rb
class Video < ActiveRecord::Base
has_attached_file :video,
:path => ":rails_root/public/system/:attachment/:id/:style/:normalized_video_file_name",
:url => "/system/:attachment/:id/:style/:normalized_video_file_name"
Paperclip.interpolates :normalized_video_file_name do |attachment, style|
attachment.instance.normalized_video_file_name
end
def normalized_video_file_name
"#{self.id}-#{self.video_file_name.gsub( /[^a-zA-Z0-9_\.]/, '_')}"
end
end
What are we doing here? Easy, in has_attached_file we edit the way paperclip returns the path and url by default, the most relevant components when saving and loading the file in order to display it. Paperclip default values are:
path default => ":rails_root/public/system/:attachment/:id/:style/:filename"
url default => "/system/:attachment/:id/:style/:filename"
Values preceded by ’:’ are the standard interpolations paperclip has
http://blog.wyeworks.com/2009/7/13/paperclip-file-rename
you need to add an :s3_headers entry to your has_attachment line:
has_attached_file :asset,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "uploads/:id/:basename.:extension",
:s3_headers => {"Content-Disposition" => "attachment"},
:s3_permissions => 'authenticated-read',
:s3_protocol => "http",
:bucket => "my_bucket_or_something"