1

我有个问题。

我试图显示一条没有属性的消息。我搜索了答案,但任何解决方案都对我有用。

我只想显示这样的错误消息:

附件库não é permitido 上传 de .exe

但始终显示:“附件库”。我只想显示:nãoé permitido upload 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>

在我的意见/布局/_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}"

有人可以帮助我吗?对不起我的英语不好。

谢谢

4

1 回答 1

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
于 2013-10-14T15:22:47.040 回答