2013-02-23 94 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