2012-09-13 25 views
0

我想只顯示屬性的錯誤,而不是某個對象在視圖中無效。我怎麼能處理這個?Rails:不顯示「<model>錯誤」是無效的。完整的消息

我目前使用的:

<% if [email protected]_konto.errors.nil? && @ozb_konto.errors.any? %> 
<div class="alert alert-error" id="error_explanation"> 
    <h3>Following errors occured:</h3>  
    <ul> 
    <% @ozb_konto.errors.full_messages.each do |error| %> 
     <li><%= error %></li> 
    <% end %> 
     </ul> 
</div> 
<% end %> 

這將顯示所有樣的錯誤,這是正確的。但它也顯示我想禁止的錯誤<model> is not valid

我該如何處理?

在此先感謝!

回答

1

由於full_messages方法是:

def full_messages 
    map { |attribute, message| full_message(attribute, message) } 
end 

你可以轉換錯誤哈希並輸出它,只要你想:

@ozb_konto.errors.to_hash # => {:email=>["can't be blank"], :password=>["can't be blank"], :name=>["can't be blank"]} e.g 
+0

感謝,偉大工程! –