我有一个问题。
我试图显示没有属性的消息。 我搜索了答案,但任何解决方案都对我有用。
我想只显示如下错误消息:
附件基础nãoetpermitido upload de .exe
但总是表明:“附件基础”。我只想表明:nãoé能上传de .exe
嗯,在我的模型中,我有一个验证:
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
在我看来:
<% 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>
在我的观看/ 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 %>
我试过了:
errors[:base] << I18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1]))
和
errors.add_to_base(I18n.t('errors.messages.suspitious_attachment', :value => attachment.path[-4..-1]))
我尝试在我的翻译文件中插入:
activerecord:
errors:
format: "%{message}"
有人可以帮帮我吗?
抱歉我的英文。
由于
答案 0 :(得分:1)
改为使用#add_to_base:
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