2013-10-14 33 views
1

我遇到問題。errors.add(:base,「MSG」)without attributes

我試圖顯示沒有屬性的消息。 我搜索了答案,但任何解決方案都爲我工作。

我想說明這樣才錯誤消息:

附件基地NAOépermitido上傳去的.exe

但總是顯示這樣的:「附件基地」。我想只是爲了證明:NAOépermitido上傳去的.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}" 

有人能幫助我嗎? 對不起,我的英語。

感謝

回答

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 
+0

您好!我已經嘗試過這種方式,但沒有奏效。 –