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
Related
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
I have a Design model that has two Paperclip attachments inside two associations (full_image and preview). I want to be able to save designs only when both full_image and preview have valid files but can't seem to be able to make it work. Right now this is what I expected to work, but it doesn't just doesn't validate the attachments when I submit the form.
class Design < ActiveRecord::Base
has_one :full_image, :as => :assetable, :class_name => "FullImage", :dependent => :destroy
has_one :preview , :as => :assetable, :class_name => "Preview" , :dependent => :destroy
accepts_nested_attributes_for :full_image, :preview
validates_associated :preview, :full_image
end
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
end
class FullImage < Asset
has_attached_file :attachment
validates_attachment_presence :attachment
end
class Preview < Asset
has_attached_file :attachment
validates_attachment_presence :attachment
end
Could someone please suggest what I should be doing?
Try :
validates :attachment, :presence => true
inside the associated model instead of validates_attachment_presence
Here is how I got it working
class Design < ActiveRecord::Base
has_one :full_image, :as => :assetable, :class_name => "FullImage", :dependent => :destroy
has_one :preview , :as => :assetable, :class_name => "Preview" , :dependent => :destroy
accepts_nested_attributes_for :full_image, :preview
validates_presence_of :preview
validates_presence_of :full_image
end
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
delegate :url, :to => :attachment
end
class FullImage < Asset
has_attached_file :attachment
end
class Preview < Asset
has_attached_file :attachment
end
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
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.
Newbie question. I have the following models:
class Asset < ActiveRecord::Base
belongs_to :assetable, :polymorphic => true
#paperclip
has_attached_file :asset,
:hash_secret => "my-secret",
:url => "/images/:hash_:basename_:style.:extension",
:path => UPLOAD_PATH + "/:hash_:basename_:style.:extension",
:styles => { :medium => "300x300>", :thumb => "75x75>"
}
end
class Location < ActiveRecord::Base
has_many :assets, :as => :assetable, :dependent => :destroy
end
class MenuItem < ActiveRecord::Base
has_many :assets, :as => :assetable
end
My asset has a property called description. If the assetable_type is a "MenuItem" and the description is nil, I'd like the description to be the associated menu_item's body. How would I do this?
thx
class Asset < ActiveRecord::Base
before_save :set_description
private
def set_description
self.description ||= assetable.body if assetable.is_a?(MenuItem)
end
end
Or modify the accessor
def description
return self[:description] unless self[:description].blank?
assetable.description if assetable.is_a? MenuItem
end