Getting error on expecting key word
class Listing < ActiveRecord::Base
if Rails.env.development?
has_attached_file :image, :styles => { :medium => "200", :thumb => "100x100>" }, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
else
has_attached_file :image, :styles => { :medium => "200", :thumb => "100x100>" }, :default_url => "default.jpg",
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
:storage => :dropbox,
:dropbox_credentials => Rails.root.join("config/dropbox.yml"),
:path => ":style/:id_filename"
end
validates :name, :description, :price, :address, :phone, presence: true
validates :price, numericality: { greater_than: 0}
validates :phone, length: { maximum: 14 }
validates_attachment_presence :image
belongs_to :user
end
Extra comma at the end of line
has_attached_file :image, :styles => { :medium => "200", :thumb => "100x100>" }, :default_url => "default.jpg",
without a comma at the end of line
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
Hope this might help you .....
class Listing < ActiveRecord::Base
if Rails.env.development?
has_attached_file :image, :styles => {:medium => "200", :thumb => "100x100>"}, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
else
has_attached_file :image, :styles => {:medium => "200", :thumb => "100x100>"}, :default_url => "default.jpg"
validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/,
:storage => :dropbox, :dropbox_credentials => Rails.root.join("config/dropbox.yml"), :path => ":style/:id_filename"
end
validates :name, :description, :price, :address, :phone, presence: true
validates :price, numericality: {greater_than: 0}
validates :phone, length: {maximum: 14}
validates_attachment_presence :image
belongs_to :user
end
Related
My model looks like:
class Photo < ApplicationRecord
has_attached_file :image,
:styles => { :small => "50x50#" },
:default_style => :small
validates_attachment :image,
content_type: { content_type: ["image/jpeg",
"image/gif", "image/png"] }
end
RAILS stores the image twice: as original size, and as resized defined in :small. I would like to store only resized image.
I believe that you can simply define a style for :original to have paperclip replace the original with that size.
:styles => { :original => '300x168>', :cropped_thumb => {:geometry => "50x50#", :jcrop => true}, ...}
Thank you, puneet18.
This model do the job:
class Photo < ApplicationRecord
has_attached_file :image,
:styles => { :original => "50x50#" },
:default_style => :original
validates_attachment :image,
content_type: { content_type: ["image/jpeg", "image/gif",
"image/png"] }
end
I want to update my styles in paperclip, but when i run the command, a error is returned. Here is the code of my styles:
image.rb
class Image < ActiveRecord::Base
belongs_to :imageable, :polymorphic => true
validates_presence_of :image
has_attached_file :image, :processors => [:ffmpeg],
:styles => { :video => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "100x100>", :format => 'jpg' },
:large => { :geometry => "400x300>", :format => 'jpg' },
:medium => { :geometry => "400x200>", :format => 'png' },
:icon => { :geometry => "36x36>", :format => 'jpg' },
:profile => { :geometry => "28x28>", :format => 'jpg' } },
:default_url => "/images/:style/missing.png"
end
Returned Error:
Regenerating Image -> image -> [:icon, :large, :medium, :profile, :thumb, :video]
rake aborted!
Cocaine::CommandNotFoundError
How i solve this error?
It seems to be that you have not installed the "paperclip-ffmpeg" gem.
See this link: https://github.com/owahab/paperclip-ffmpeg#requirements
When I try to upload a video using Paperclip, I get an error message can't dump File.
Model Video :
class Video < ActiveRecord::Base
has_attached_file :avatar,
:storage => :s3,
:styles => {
:mp4 => { :geometry => "640x480", :format => 'mp4' },
:thumb => { :geometry => "300x300>", :format => 'jpg', :time => 5 }
}, :processors => [:ffmpeg]
validates_attachment_presence :avatar
validates_attachment_content_type :avatar,
:content_type => /video/,
:message => "Video not supported"
end
Added the convert_options code to existing Paperclip code in my user method. Now getting:
/Users/-----/----/-------/app/models/user.rb:148: syntax error, unexpected tASSOC, expecting keyword_end
:convert_options => { :small => '-quality 40' }
What am I missing?
User Model:
...
# Paperclip
has_attached_file :photo,
:styles => {
:small => ["50x50#", :jpeg],
:big => ["450x450#", :jpeg]
}
:convert_options => {
:small => '-quality 40'
}
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
...
You're missing a comma after your :styles => {...}.
has_attached_file :photo,
:styles => {
:small => ["50x50#", :jpeg],
:big => ["450x450#", :jpeg]
},
:convert_options => {
:small => '-quality 40'
}
I know that it's a lot of indenting but it helps me when I try to debug some thing.
Some editors highlight opening brackets with closing ones. It can also help.
How can I do something inside the model depending on the whether it's creating or updating a record? I'm sure it's really simple, but I can't seem to figure it out.
These are different styles for updating or creating attachments with paperclip.
class Photo < ActiveRecord::Base
belongs_to :product
#after_upate :flag_somthin
Paperclip.interpolates :product_id do |attachment, style|
attachment.instance.product_id
end
has_attached_file :data,
:storage => 's3',
:s3_credentials => "#{RAILS_ROOT}/config/s3_credentials.yml",
:bucket => 'leatherarts.com',
:s3_host_alias => 'leatherarts.com.s3.amazonaws.com',
:url => ':s3_alias_url',
:path => "images/products/:product_id/:style/:basename.:extension",
:styles => lambda { |style| style.instance.choose_styles },
:default_style => :medium,
:default_url => 'http://leatherarts.com.s3.amazonaws.com/images/records/m1.png',
:s3_headers => { 'Expires' => 2.months.from_now.httpdate }
validates_attachment_presence :data
validates_attachment_size :data, :less_than => 10.megabytes
validates_attachment_content_type :data, :content_type => ['image/jpeg','image/gif','image/png']
def choose_styles
{ :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>", :backup => "2000x2000>" }, :on => :create
{ :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>" }, :on => :update
end
end
Use the new_record? method to return different hashes:
def choose_styles
defaults = { :thumb => "60x60#", :small => "200x200>", :medium => "400x400>", :large => "1000x1000>" }
defaults.merge! :backup => "2000x2000>" if new_record?
defaults
end