我正在處理創建新對話的傳統表單。它有兩個字段:名稱和描述(對話的第一個評論)無法將文本區域樣式更改爲field_with_error
這裏是字段: _fields.haml
.conversation_title= f.label :name, t('.name')
.clear
= f.text_field :name, :style => 'width: 230px'
= errors_for f.object, :name
if f.object.new_record?
= f.fields_for :comments, f.object.comments.build do |comment_fields|
.conversation_title= comment_fields.label :description, t('.description')
= comment_fields.text_area :body, :placeholder => t("comments.new.conversation"), :style => 'width: 545px'
= errors_for f.object, :comments
從new
視圖對話
= form_for [@current_project, @conversation], :html => { 'data-project-id' => @current_project.id, :name => 'form_new_conversation', :multipart => true } do |f| #, :onsubmit => 'return validate_form_new_conversation(form_new_conversation)'
= render 'fields', :f => f, :project => @current_project
= render 'watcher_fields', :f => f, :project => @current_project
相關的驗證是:
conversation.rb
validates_presence_of :name, :message => :no_title, :unless => :simple?
validates_presence_of :comments, :message => :must_have_one, :unless => :is_importing
comment.rb
validates_presence_of :body, :unless => lambda { |c| c.task_comment? or c.uploads.to_a.any? or c.google_docs.any? }
出於某種原因,從base.rb
@@field_error_proc = Proc.new{ |html_tag, instance| "<div class=\"field_with_errors\">#{html_tag}</div>".html_safe }
相關聯的錯誤領域PROC不會被調用爲文本區,所以它不會改變它的風格讓它變成紅色。它適用於:name
字段。錯誤信息得到正確顯示
我錯過了什麼? 謝謝!
我在我的問題的描述中添加了'Comment'model驗證。在調試時,顯然comment_fields.object沒有錯誤。因此,在評論主體字段下顯示的消息對應於對話的驗證。調試還顯示,多次調用相同的驗證。這是正常的嗎? – Arthur
@arthur如果在嵌套表單的情況下多次觸發驗證,我不會感到驚訝。我會盡量避免它,但這不會是我最關心的問題。另外,我懷疑這是造成任何問題的原因。 –