我有一個調用投訴表單的視圖。它也恰好在同一頁上有部分內容。Rails表單驗證不適用於並且update - view有表單並且部分
如果我創建一個新的投訴,驗證按預期工作。但是,如果我在更新期間刪除驗證字段的內容並嘗試更新/保存,則會出現崩潰/錯誤,但我沒有收到任何驗證錯誤。
class Complaint < ActiveRecord::Base
belongs_to :customer, :class_name => 'Customer', :foreign_key => 'customer_id'
has_many :attachments, :dependent => :destroy
accepts_nested_attributes_for :attachments
has_many :document_histories #, :dependent => destroy
accepts_nested_attributes_for :document_histories
# has_one :user
has_many :users, :through => :document_histories
validate :resolution_must_be_in_the_future
validates_presence_of :status
validates_presence_of :sales_order
validates_presence_of :product_name
validates_presence_of :coater_roll_number
validates_presence_of :coater_work_order
validates_presence_of :slitter_disposition
validates_presence_of :length
validates_presence_of :measure
validates_presence_of :disposition
validates_presence_of :reason_for_complaint
validates_presence_of :customer
after_initialize :init
def init
if self.new_record? && self.status.nil? # see if it's a new record and if it is nill
self.status = 'New' # Set status - NOTE - in this case we used a dropbox collection so the setting should be something in the dropbox .
end
end
def resolution_must_be_in_the_future
errors.add(:resolution_date, "must be in the future") if
!requested_resolution_date.blank? && requested_resolution_date < Date.today && self.new_record?
end
end
以下是我投訴表單中的兩個字段。當然,還有更多的領域。
<div class="field">
<strong><%= f.label :slitter_disposition %></strong><br>
<%= f.text_area :slitter_disposition, :rows => 10, :cols => 120 %>
</div>
<div class="field">
<strong><%= f.label :reason_for_complaint %></strong><br>
<%= f.text_area :reason_for_complaint, :rows => 5, :cols => 120 %>
</div>
如果我在進行更新時刪除任何驗證字段的內容,我會收到以下錯誤消息。在這種情況下
NoMethodError in Complaints#update
Showing C:/Users/cmendla/RubymineProjects/internal_complaints/app/views/complaints/_form.html.erb where line #41 raised:
undefined method `name' for nil:NilClass
Trace of template inclusion: app/views/complaints/edit.html.erb
Rails.root: C:/Users/cmendla/RubymineProjects/internal_complaints
Application Trace | Framework Trace | Full Trace
app/views/complaints/_form.html.erb:41:in `block in _app_views_complaints__form_html_erb__591273067_68586864'
app/views/complaints/_form.html.erb:2:in `_app_views_complaints__form_html_erb__591273067_68586864'
app/views/complaints/edit.html.erb:89:in `_app_views_complaints_edit_html_erb__1060012553_73607220'
app/controllers/complaints_controller.rb:177:in `block (2 levels) in update'
app/controllers/complaints_controller.rb:160:in `update'
Request
Parameters:
{... removed irrelevent parameters
"reason_for_complaint"=>"",
我的日誌文件顯示..
Rendered complaints/_form.html.erb (6.0ms)
Rendered complaints/edit.html.erb within layouts/application (22.0ms)
Completed 500 Internal Server Error in 35ms (ActiveRecord: 0.0ms)
ActionView::Template::Error (undefined method `name' for nil:NilClass):
40: -->
41:
42: <% if action_name != "new" && action_name != "create" then %>
43: <%= @user.name %>
44: <% else %>
45: <%= @current_user.name %>
46: <%= f.hidden_field(:user_id, :value => @current_user.id) %>
app/views/complaints/_form.html.erb:43:in `block in _app_views_complaints__form_html_erb__591273067_73900308'
app/views/complaints/_form.html.erb:2:in `_app_views_complaints__form_html_erb__591273067_73900308'
app/views/complaints/edit.html.erb:89:in `_app_views_complaints_edit_html_erb__1060012553_73607220'
app/controllers/complaints_controller.rb:177:in `block (2 levels) in update'
app/controllers/complaints_controller.rb:160:in `update'
不管是什麼領域我留空,錯誤總是指向同一行的上方。我如何確保Rails驗證在我進行更新時有效?
--------------編輯
如果我註釋掉爲空,則更新過程沒有錯誤領域的驗證。即如果我說出行
# validates_presence_of :reason_for_complaint
然後窗體將處理,該字段將在表中爲空。問題是,該驗證需要
------------------------編輯2
這裏是我的代碼得到錯誤。如果這是新的投訴,則代碼不應執行。沒有驗證錯誤時,它不會執行更新。看起來驗證錯誤導致表單認爲它是新的/不創建編輯/更新。
<td>
<% if action_name != "new" && action_name != "create" then %>
<% $document_assign = @complaint.id %>
<% $sent_by_name = $current_user_name %>
<% $sent_by_login = @user.login %>
<% end %>
</td>
我想我看到發生了什麼。我在周圍的原始文章中添加了周圍的代碼。當我正在進行編輯/更新時,我得到錯誤的代碼不應該執行,但是,當我驗證失敗時,它正在執行。我正在查看編輯和更新控制器。 –