Show error message using validate

120 Views Asked by At

I'm having difficulty displaying an error message on the screen.

I have a class called Issue, and this has several attachments.

No model/issue_attachment, I am checking the files to determine if they are supported. In the case that the file name are not supported I am displaying the number 1 on the screen (just for testing, I will improve).

Only it is not showing "LOL" on the screen. I know I can do the validation and display the error message on model/issue_attachment, but in this case I need to do so.

class IssueAttachment < ActiveRecord::Base
  belongs_to :issue, counter_cache: :attachments_count

  has_attached_file :attachment, :url => '/system/:issue_attachment_directory/:filename'

  validates_attachment_size :attachment, :less_than => 10.megabytes, :message =>    I18n.t('errors.messages.issue_attachment_file_size_html', :max => '10 MB')
  validate :suspitious_attachment


def suspitious_attachment
 if ends_with? '.bat', '.com', '.exe', '.src', '.cmd'
   1
 end
 1
end

private

def ends_with?(*args)
  args.each do |arg|
    return true if attachment.path.ends_with? arg
  end
  false
  end  
end

In model/issue, I have:

class Issue < ActiveRecord::Base
 ...
accepts_nested_attributes_for :attachments, allow_destroy: true
validate :suspitious_attachment02

private

def suspitious_attachment02
  attachments.all.each do |attachment|
    if attachment.suspitious_attachment == 1
      errors.add(:attachments, "LOL")
    end 
  end
end

Could someone help me? It's possible do this?

Thanks =]

Solution

In model/issue.rb, to change

def suspitious_attachment02
  attachments.all.each the | attachment |

for

def suspitious_attachment02
  attachments.each the | attachment |
0

There are 0 best solutions below