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.
Related
I am trying to have a polymorphic attachment class with dynamic styles using paperclip and use it form another class(Foo).
I have the Attachment class:
class Attachment < ApplicationRecord
belongs_to :attachable, polymorphic: true, optional: true
has_attached_file :attachment,
styles: ->(a) {a.instance.attachable.attachment_styles}
validates_attachment_content_type :attachment, content_type: /\Aimage\/.*\Z/
end
And the Foo class:
class Foo < ApplicationRecord
has_many :attachments, class_name: "Attachment", as: :attachable, dependent: :destroy
accepts_nested_attributes_for :attachments
def attachment_styles
{
:thumb => "30x30>",
:medium => "100x100>"
}
end
end
The problem here is that I don't have the foo object yet, inside that lambda function. a.instance.attachable returns nil.
The error I'm getting is:
NoMethodError: undefined method `attachment_styles' for nil:NilClass
I am using Rails 5 and paperclip 5.0.0.
I think this worked with Rails 4.
PS: I need the style to be dynamic because I will use the attachment from multiple models, not just from Foo.
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' }
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.
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.
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.