PaperClip for Two Models Error? - ruby-on-rails

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

Related

How to make a model has unlimited images with paperclip in Ruby on Rails?

I have a User model that has an avatar. Paperclip was used to allow image upload. However, I want a User to be able to upload as many images as possible (unlimited) . How do I modify my model to allow such behavior ?
The user model looks like this:
class Model < ApplicationRecord
has_attached_file :pic, styles: { medium: "420×633!", thumb: "100x100#" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :pic, content_type: /\Aimage\/.*\z/
has_many :reviews, dependent: :destroy
Thanks in advance !
You can store the photos (if you call it that) for a user in a separate model and add an association to it in User model:
Command line
rails g paperclip photo pic
app/models/user.rb
has_many :photos, dependent: :destroy
app/models/photo.rb
belongs_to :user
has_attached_file :pic, styles: { medium: "420×633!", thumb: "100x100#" }, default_url: "/images/:style/missing.png"
validates_attachment_content_type :pic, content_type: /\Aimage\/.*\z/
As per the given specifications you could generate another model for saving user_images, in this way user can have unlimited images.
user.rb
class User < ApplicationRecord
has_many :user_images, dependent: :destroy
accepts_nested_attributes :user_images
validates_attachment_content_type :pic, content_type: /\Aimage\/.*\z/
has_many :reviews, dependent: :destroy
end
user_image.rb
Class UserImage < ApplicationRecord
belongs_to :user
has_attached_file :user_image, styles: { medium: "420×633!", thumb: "100x100#" }, default_url: "/images/:style/missing.png"
end
Added accepts_nested_attributes in user_model and paperclip setting in user_image for uploading images.

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' }

Could not find generator AddListingIdToOrders

Trying to add an id to my orders database
when i run the migration at the command line $ rails generate AddListingIdToOrders listing_id:integer
it gives me an error Could not find generator AddListingIdToOrders.
cmd neilpatel$ rails generate AddListingIdToOrders listing_id:integer
Could not find generator AddListingIdToOrders.
listing.rb
class Listing < ActiveRecord::Base
if Rails.env.development?
has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "photo.jpg"
else
has_attached_file :image, :styles => { :medium => "200x", :thumb => "100x100>" }, :default_url => "photo.jpg",
:storage => :dropbox,
:dropbox_credentials => Rails.root.join("config/dropbox.yml"),
:path => ":style/id_:filename"
end
validates :name, :description, :price, presence: true
validates :price, numericality: { greater_than: 0 }
validates_attachment_presence :image
belongs_to :user
has_many :orders
end
orders.rb
class Order < ActiveRecord::Base
validates :address, :city, :state, presence: true
belongs_to :listing
end
The command you're looking for is
rails generate migration AddListingIdToOrders listing_id:integer
You're missing the 'migration' part :)

Paperclip: Product with multiple Attachments

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

How to create pages by the click of a link in ruby on rails?

I'm following this tutorial: http://railscasts.com/episodes/253-carrierwave-file-uploads or atleast I want to but would like to know if there are any tutorials around that explain how to give my users the ability to create pages(galleries) on the fly?
I intend to treat pages as albums.
They click create album link, fill in an album title.
A new page is created and from this page the user can upload photos onto the page.
kind regards
Albums and photos are just simple models. You can create controllers for them. Here is little example:
class Album < ActiveRecord::Base
belongs_to :user
has_many :album_works
validates :title, :description, :user_id, :presence => true
attr_accessible :title, :description
end
And for album work:
class AlbumWork < ActiveRecord::Base
belongs_to :album
has_many :album_work_comments
has_attached_file :photo,
:styles => {
:preview=> "860x",
:slider => "618x246#",
:thumb => "315x197#",
:homework_main => "532x355#",
:homework_mini => "184x122#",
:big_preview => "800x600#"
},
:path => ":rails_root/public/system/album_works/:style_:id.:extension",
:url => "/system/album_works/:style_:id.:extension",
:default_url => "/images/photo_holder.png"
validates_attachment_size :photo, :less_than => 2.megabytes
validates_attachment_content_type :photo, :content_type => ['image/png', 'image/jpeg', 'image/jpg', 'image/bmp']
attr_accessible :title, :photo
validates :title, :album_id, :presence => true
end
Now you should create corresponding controllers and views. But they are just simple rails controllers and views. Note that I'm using paperclip, but it's only an example to show how it can be done.

Resources