Could not find generator AddListingIdToOrders - ruby-on-rails

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 :)

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

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

Why does my Paperclip validates_attachment_presence test fail?

In my Rails 3.2.2 project, I have the following:
class Photo < ActiveRecord::Base
belongs_to :album
default_scope order: :title
extend FriendlyId
friendly_id :title, :use => :slugged
validates :title, :presence => true
validates :title, :uniqueness => {:scope => :album_id}
validates :file, :attachment_presence => true
has_attached_file :file, :path => (Rails.root + "photos/:id/:style/:filename").to_s,
:url => "/photos/:style/:id",
:styles => { :small => "450x450>"}
end
class PhotoTest < ActiveSupport::TestCase
should belong_to(:album)
should validate_presence_of(:title)
should have_attached_file(:file)
should validate_attachment_presence(:file)
end
The 'should validate_attachment_presence(:file)' test always flunks, but I can't figure out why. I have other unit tests with required attachments that test out fine.
Any ideas?
For me the problem disappeared after I upgraded to Paperclip 3.0.3 - seems like the bug is now fixed.

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