English 中文(简体)
在纸张片上使用录像/录像
原标题:Use single attachment for video/image in paperclip

I am using paperclip for uploading file ( video and images). Have used the same attachment(source) for both video and images.

class Media < ActiveRecord::Base
  belongs_to :memory
  validates_attachment_presence :source
  validates_attachment_content_type :source,
    :content_type => [ video/mp4 ,  image/png ,  image/jpeg ,  image/jpg ,  image/gif ]
end

现在我想在不同案件中显示不同的错误信息。

  1. When uploading file is image type but not the jpg/png/jpeg/gif.
  2. When uploaded file is video type but not the mp4

How can i achieve this ? Any help would highly appreciated.

最佳回答

So finally I got the solution. I added 2 conditional validation for the same

class Media < ActiveRecord::Base
  belongs_to :memory
  validates_attachment_presence :source
  validates_attachment_content_type :source,
    :content_type => [ video/mp4 ],
    :message => "Sorry, right now we only support MP4 video",
    :if => :is_type_of_video?
  validates_attachment_content_type :source,
     :content_type => [ image/png ,  image/jpeg ,  image/jpg ,  image/gif ],
     :message => "Different error message",
     :if => :is_type_of_image?
  has_attached_file :source

  protected
  def is_type_of_video?
    source.content_type =~ %r(video)
  end

  def is_type_of_image?
    source.content_type =~ %r(image)
  end
end
问题回答

暂无回答




相关问题
Bind Button.IsEnabled to custom validation with XAML?

I am sorry I didn t know how to title my question any better, you name it if you got a good 1. I have an entity Contact. this person has navigation properties: Address, Phones (A collection of Phone)....

WPF - MVVM - NHibernate Validation

Im facing a bit of an issue when trying to validate a decimal property on domain object which is bound to a textbox on the view through the viewmodel. I am using NHibernate to decorate my property on ...

Wpf Combobox Limit to List

We are using Wpf Combobox to allow the user to do the following things: 1) select items by typing in the first few characters 2) auto complete the entry by filtering the list 3) suggesting the first ...

Rails 101 | validates_currency?

I ve searched high and low, but I could not find a solution, to what I think seems like a very common task. In a form I want to have a text input that accepts currency strings (i.e. $1,000,000 or ...

CodeIgniter form verification and class

I m using the form validation library and have something like this in the view <p> <label for="NAME">Name <span class="required">*</span></label> <?...

热门标签