Use #add_to_base instead:
def suspitious_attachment
if ends_with? '.bat', '.com', '.exe', '.src', '.cmd'
errors.add_to_base(I18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1]))
end
end
Question
I have a problem.
I have trying to show a message without attribute. I searched for answer's but any solution did work for me.
I want to show only error message like this:
Attachments base não é permitido upload de .exe
But always show this: "Attachments base". I want just to show: não é permitido upload de .exe
Well, in my model I have a validation:
validate :suspitious_attachment
private
def suspitious_attachment
if ends_with? '.bat', '.com', '.exe', '.src', '.cmd'
errors.add(:base, I18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1]))
end
end
def ends_with?(*args)
args.each do |arg|
return true if attachment.path.ends_with? arg
end
false
end
In my view:
<% if @issue.errors[:attachments].any? %>
<div class="clearfix error">
<% else %>
<div class="clearfix">
<% end %>
<label>
<%= image_tag 'icons/required.png', :alt => '*' %><%= t('labels.attachments') %>
</label>
<div class="input">
<div class="inputs-list">
<%= f.fields_for :attachments do |builder| %>
<%= render 'attachment_fields', :f => builder %>
<% end %>
<%= f.link_to_add t("actions.add_another_attachment"), :attachments, :class => "icon-attach" %>
<% if @issue.errors[:attachments].any? %>
<%= content_tag :span, t('errors.messages.blank'), class: 'help-inline error' %>
<% end %>
</div>
</div>
In my views/layout/_error_messages.html.erb
<% if target.errors.any? %>
<% if target.errors.any? %>
<div class="alert-message fade in warning">
<a class="close" href="#">×</a>
<p><%= target.errors.full_messages.first %></p>
</div><% end %>
I tried:
errors[:base] << I18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1]))
and
errors.add_to_base(I18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1]))
I tried to insert in my translate file:
activerecord:
errors:
format: "%{message}"
Someone can help me? Sorry for my english.
Thanks
Solution
Use #add_to_base instead:
def suspitious_attachment
if ends_with? '.bat', '.com', '.exe', '.src', '.cmd'
errors.add_to_base(I18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1]))
end
end