Why doesn't work Paperclip interpolations? - ruby-on-rails

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

Related

Adding Paperclip Attachment to Spree Orders Table,

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

RAILS S3 displaying pdf file stored in amazon s3

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"

paperclip - modify the path structure for storing images

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",

Amazon S3 path or to_file wont work

Hey guys ive got the following code in my Model
require 'RMagick'
class Upload < ActiveRecord::Base
belongs_to :card
has_attached_file :photo,
:styles => {
:thumb => ["100x100", :jpg],
:pagesize => ["500x400", :jpg],
},
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'your_deck',
:default_style => :pagesize
attr_accessor :x1, :y1, :width, :height
def update_attributes(att)
scaled_img = Magick::ImageList.new(self.photo.path)
orig_img = Magick::ImageList.new(self.photo.path(:original))
scale = orig_img.columns.to_f / scaled_img.columns
args = [ att[:x1], att[:y1], att[:width], att[:height] ]
args = args.collect { |a| a.to_i * scale }
orig_img.crop!(*args)
orig_img.write(self.photo.path(:original))
self.photo.reprocess!
self.save
super(att)
end
end
This code works offline however when on Heroku with Amazon S3 it fails to work, ive tried the code with to_file and it also wont work
I get the following error
can't convert Array into String
I had the same problem and it's due to an update to paperclip. I'm surprised heroku are still using this gem version as it will surely affect all their users; I installed a previous version as a plugin and it's fine. Don't forget to remove the gem from your .gems file or specify the previous version in your gems manifest.

How to pass additional convert options to paperclip on Heroku?

class User < ActiveRecord::Base
has_attached_file :photo, :styles => { :square => "100%", :large => "100%" },
:convert_options => {
:square => "-auto-orient -geometry 70X70#",
:large => "-auto-orient -geometry X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
validates_attachment_size :photo,
:less_than => 5.megabyte
end
Works great on local machine, but gives me an error on Heroku: There was an error processing the thumbnail for stream.20143
The thing is I want to auto-orient photos before resizing, so they resized properly.
The only working variant now(thanks to jonnii) is resizing without auto-orient:
...
as_attached_file :photo, :styles => { :square => "70X70#", :large => "X300" },
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => ":attachment/:id/:style.:extension",
:bucket => 'mybucket'
...
How to pass additional convert options to paperclip on Heroku?
UPD
I discover, the trouble in "-auto-orient" option. It seems like this option is broken in version of ImageMagick used by Heroku. I created custom paperclip image processor inherited from paperclip's standard thumbnail:
module Paperclip
class Ao < Thumbnail
def transformation_command
super + " -auto-orient"
end
end
end
It works perfect on local machine, but fails on Heroku.
These are the sizes I use. They all work fine on heroku:
SIZES = {
:original => "640x480>",
:thumb => "150x150#",
:mini => "60x60#",
:micro => "30x30#"
}
Make sure your gem version of paperclip is the same as heroku's. You can specify the specific gem version in your .gems file and in your environment.rb to make sure they line up.
I'm not sure exactly why your convert_options are causing problems, but if I remember correctly paperclip uses ImageScience directly and your chosen options might be incompatible with the read only heroku file system.
If this is critical and you need an answer right now I'd raise a support ticket on heroku. If you get a response make sure you post it back here!

Resources