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.
Related
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' }
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 :)
I'm trying to use Paperclip for uploading an image for an has many and belongs to relationship with 'pictures'
I'm certain that imagemagick etc, is correctly installed, because i've more image uploaders in this project that works, difference is that this one has an has many relation.
I'm not getting a 'Saving Attachment' in the console. So my idea is that this is completely ignored by the strong parameters.
In topicscontroller:
params.require(:topic).permit(:id, :title, :content, :active, pictures_attributes: [:image, :id, :_destroy] )
In Topic Model:
has_many :pictures, dependent: :destroy
accepts_nested_attributes_for :pictures, allow_destroy: true
validates :title, :content, presence: true
In Picture Model:
belongs_to :topic
has_attached_file :image,
:styles => { :medium => "400x400#", :thumb => "200x200#", :small => "50x50#" },
:default_url => ActionController::Base.helpers.asset_path('missing.png')
I know that there are many other topics about this, only all are rails 3 and is different in the way of setting 'attr_accessible'
With version 3.5.0 of Paperclip, everything is fine for Rails 4.
Model Code (Attempting to require uniqueness for embed_code)
https://gist.github.com/1427851:
class Link < ActiveRecord::Base
validates :embed_code,
:format => {:with => /^<object type|^<embed src|^<object width|^<iframe src|^<object height|^<iframe width|^<embed id|^<embed width|^<object data|^<div|^<object id/i, :message => "Invalid Input"},
:uniqueness => true
attr_accessible :title, :embed_code, :score
after_initialize :calculate_score, :favs_count
attr_accessor :score, :fav_count
validates :title, :length => { :in => 4..45, :message => "Must be between 4 & 45 characters"}
before_save :resize
has_many :favorites
has_many :favorited, :through => :favorites, :source => :user
belongs_to :user
I've tried validates_uniqueness_of :embed_code and disabling (commenting out) non-critical components of the model such as :before_save :resize
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.