在rails4中获取相同的错误消息2次回形针

时间:2015-06-09 07:56:56

标签: ruby-on-rails validation

您好我已经提供了我的附件模型:

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'],
                                    :if => Proc.new { |f| f.attachable_type == 'Interview' }
  validates_attachment_content_type :attach,
                                    :content_type => ['image/jpeg', 'image/jpg', 'image/png'],
                                    :if => Proc.new { |f| f.attachable_type == 'Designation' || 'Department' },
                                    :message => I18n.t('content_type', :scope => 'paperclip.errors.attachment')

end

在我的控制器动作中定义了这个:

 def create
    @designation = Designation.create(designation_params)
   if @designation.errors.present?
     p @designation.errors
      render :action => :new
    else
      redirect_to designations_path, notice: I18n.t('designation_created')
    end
  end
指定模型中的

代码:

has_one :attachment, as: :attachable
  accepts_nested_attributes_for :attachment

在en.yml

en.
 paperclip:
    errors:
      attachment:
        content_type: The image format you are trying to upload is not supported. Please upload an image of format JPG, GIF or PNG
        presence: Cant' be blank

因此,无论何时出现错误,我都会在控制台中显示两次

  

     

@messages = {:&#34; attachment.attach_content_type&#34; =&gt; [&#34;图片格式你   正在尝试上传不受支持。请上传图片   格式JPG,GIF或PNG&#34;],:&#34; attachment.attach&#34; =&gt; [&#34;图像格式你   正在尝试上传不受支持。请上传图片   格式JPG,GIF或PNG&#34;]}&gt;

请指导我如何克服此错误。提前谢谢。

1 个答案:

答案 0 :(得分:0)

此代码表示如果可附加类型为“指定”或“部门”字符串是否评估为true(我保证可以)。

Proc.new { |f| f.attachable_type == 'Designation' || 'Department' }

可能你曾尝试写过像

这样的东西
Proc.new { |f| f.attachable_type == 'Designation' || f.attachable_type ='Department' }