2013-02-23 91 views
10

在这里有一个小问题。errors.full_messages format in rails 3

我的Flash通知/警报里面[" "]

enter image description here

在我的控制,我必须表明的错误,如果不保存表单。

format.html { redirect_to new_project_procurement_management_plan_path, alert:"#{@project_procurement_management_plan.errors.full_messages}"} 

下面是如何插入Flash的观点:

_alert.html.erb

<% [:notice, :error, :alert].each do |level| %> 
    <% unless flash[level].blank? %> 
    <div class="alert alert-<%= flash_class(level) %>" id="flash"> 
     <a class="close" data-dismiss="alert" href="#">×</a> 
     <%= content_tag :p, flash[level] %> 
    </div> 
    <% end %> 
<% end %> 

而且在我的帮助文件:

#Flash Message 

    def flash_class(level) 
     case level 
     when :notice then "success" 
     when :error then "error" 
     when :alert then "error" 
    end 
    end 

现在怎么能我删除了我的错误显示[" "]

任何人都知道在哪里配置它?谢谢。

编辑

这是在我的模型验证消息:

def equality 
    self.items.each do |item| 
     errors.add(:base, "#{item.description.capitalize}: Quantity must be equal to the breakdown of quantity!") if item.months != item.qty 
    end 
    end 

回答

24

errors.full_messages返回所有错误消息的数组这就是为什么你看到的括号和引号。您可以使用.to_sentence将该数组转换为可读的句子。

@project_procurement_management_plan.errors.full_messages.to_sentence

+0

谢谢。当我使用这个来填充警告消息使用JavaScript它使用有趣的符号:“建设可以'吨空白”--->任何想法如何我可以摆脱“&#39”任何指针将不胜感激 – BKSpurgeon 2016-08-04 06:16:29

+0

看起来就像javascript试图为你清理字符串一样,你可能只需要使用'unescape()'或类似的东西来使格式恢复正常 – 2016-08-04 13:25:06

+0

要添加到你的答案,我发现检查什么是返回是有帮助的。我会在控制器中使用调试语句来查看'@project_procurement_management_plan.errors.full_messages'返回的结果,或者在'Rails'控制台中返回相同结果。 – Tass 2017-01-23 18:32:15