Paperclip: Product with multiple Attachments - ruby-on-rails

I have a Product model that looks like this:
class Product < ActiveRecord::Base
attr_accessible :name_en, :ean
has_many :images, :dependent => :destroy
end
and an Images model that looks like this
class Images < ActiveRecord::Base
attr_accessible :image, :product_id, :source
has_attached_file :image, :styles => { :medium => "150x150>" },
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:bucket => "test",
:path => "/products/:ean/:style.:extension";
belongs_to :product
end
What is the easiest way to save images from an url to s3 and can I use the :ean value from the Product model for the s3 path?

use the active record callbacks.....
before_save :push_to_s3
or
after_save :push_to_s3
and then you just define your push to s3 function

Related

Validate attachment format for specific models in Rails 4

I have a polymorphic association with an Attachment model with profiles and documents tables. I have included following code in my attachment.rb:
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
has_attached_file :attach,
:storage => :s3,
:s3_credentials => "#{Rails.root}/config/s3.yml",
:url => ":s3_domain_url",
:path => "/contents/:id/:basename.:extension"
validates_attachment_content_type :attach,
:content_type => ['application/msword',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'application/pdf',
'image/jpeg', 'image/jpg', 'image/png']
end
and in my profile.rb
class Profile < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments
end
in my document.rb
class Document < ActiveRecord::Base
has_many :attachments, as: :attachable
accepts_nested_attributes_for :attachments
end
My requirement is when I save my profile it will validate only the images format, and when I save documents it will validate only the application format. Please guide me on how to solve this.
attachment.rb
validates_attachment :attach, :presence => true, :with => %r{\.(jpeg|jpg|png)$}i, :if => Proc.new{|f| f.attachable_type == 'Profile' }

PaperClip for Two Models Error?

I installed Paperclip for my first Model and its working fine but when i try to add it to my second Model i get an error . I am basically trying to have two image uploading for my two Models that i have created. This is the Error:
undefined method `image_content_type' for #<IosCourse:0x007fd4bb3bfaf0>
This is my first model (Rubycourse.rb):
class Rubycourse < ActiveRecord::Base
acts_as_votable
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
has_many :reviews
end
This is the second model (IosCourse.rb):
class IosCourse < ActiveRecord::Base
attr_accessor :image_file_name
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
You should add the needed columns (for Paperclip) to the second model as well.
Paperclip will wrap up to four attributes (all prefixed with that attachment's name, so you can have multiple attachments per model if you wish) and give them a friendly front end. These attributes are:
|attachment|_file_name
|attachment|_file_size
|attachment|_content_type
|attachment|_updated_at
So, basically you need to write/run a migration to add those attributes to the second model:
class AddImageColumnsToIosCourse < ActiveRecord::Migration
def self.up
add_attachment :ios_courses, :image
end
def self.down
remove_attachment :ios_courses, :image
end
end
Paperclip provides a migration generator to generate that file:
$ rails generate paperclip IosCourse image
Another idea: If you'll have different models with attachments, and these attachments will have similar logic (validations, extra methods, ...), it's probably a good idea to create a polymorphic model (ie. Attachment) with all these Paperclip logic and associate this new model with the rest of your models.
class Attachment < ActiveRecord::Base
belongs_to :attachable, polymorphic: true
# Paperclip stuff
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
end
class Rubycourse < ActiveRecord::Base
has_one :attachment, as: :attachable
end
class IosCourse < ActiveRecord::Base
has_one :attachment, as: :attachable
end

interpolating polymorphic relation type in url paperclip

I have a photo model which is polymorphic
class Photo < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
has_attached_file :image,
:styles => {:thumb => '120x120>', :medium => '640x480>' },
:default_style => :thumb,
:url => "/uploads/photos/#{model.imageable_type.to_s.pluralize.underscore}/:id/:basename.:extension"
and
class Question < ActiveRecord::Base
has_one :photo, as: :imageable, dependent: :destroy
class Answer < ActiveRecord::Base
has_one :photo, as: :imageable, dependent: :destroy
this generates the url as uploads/photos/Photo/some_id/file_name.ext
but I want
public/uploads/photos/questions/1/image.jpg
and
public/uploads/photos/answers/1/image.jpg.
Solved it myself.
:url => "/uploads/photos/:imageable_name/:id/:basename.:extension"
Paperclip.interpolates :imageable_name do |attachment, style|
attachment.instance.imageable_type.pluralize.downcase
end

Nested forms dont work in rails_admin

Brand has many images is my association. Image uses paperclip
In Rails Admin, I want to add images when i add a brand
class Brand < ActiveRecord:Base
has_many :images, as=> :imageable
end
class Image < ActiveRecord:Base
attr_accessor :image_thumb
attr_accessible :image, :imageable_id, :imageable_type, :image_thumb
has_attached_file :image, :styles => { :medium => "300x300>", :thumb => "100x100>" }
belongs_to :imageable, :polymorphic => true
end
But this is what i get
How can i achieve my goal?
I need to add attr_accessible
class Brand < ActiveRecord::Base
attr_accessible :name, :images_attributes
has_many :products
has_many :images, :as => :imageable
accepts_nested_attributes_for :images, :allow_destroy => true
end
And solution is when you user acts_as_taggable, to add to your rails_admin.rb config file next code:
field :tag_list do
label "Tag"
end
Or 'image_list', depends on what you are use.

nested attributes paperclip

Use paperclip for my images.
Models:
class Country < ActiveRecord::Base
has_many :regions
has_many :assets, :dependent => :destroy
accepts_nested_attributes_for :assets
end
class Asset < ActiveRecord::Base
belongs_to :country
has_attached_file :image,
:styles => {
:thumb=> "100x100>",
:small => "300x300>",
:large => "600x600>"
}
end
My country index.html looks like this:
countries.each do |country|
country.name
I tried this:
link_to( image_tag(country.asset.image.url(:thumb)), country.asset.image.url(:original) )
But I get an error.
Someone ideas, what i am doing wrong?
You have got MANY assets for each country
countries.each do |country|
country.name
country.assets.each do |asset|
link_to( image_tag(asset.image.url(:thumb)), asset.image.url(:original) )
Or change it to has_one association

Resources