我有一个调用投诉表格的观点。它也碰巧在同一页面上有部分内容。
如果我创建新投诉,则验证按预期工作。但是,如果我在更新期间删除已验证字段的内容并尝试更新/保存,则会出现崩溃/错误,但我没有收到任何验证错误。
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>
答案 0 :(得分:1)
这里似乎存在一些潜在问题,如果没有更多代码,很难说明验证是如何发挥作用的。
让我们先来看看您的服务器日志。它似乎告诉我们的是:你的代码试图在{0}上调用{ - 1}} - 这是不好的。
这条线,是它的borking线:
.name
app/views/complaints/_form.html.erb:43:in block in
如果我们查看模板代码中的第43行,您似乎试图在_app_views_complaints__form_html_erb__591273067_73900308'
上致电.name
。好的,这意味着@user
在那时是零。奇怪的。那为什么会这样?
那么,您的第一笔业务是确定为什么@user
在那时会为零?您是否有机会单步执行控制器代码并确保在将@user
变量发送到View之前设置它?
答案 1 :(得分:0)
@queserasera got me pointed in the correct direction. It appears that in the event of a validation failure, I need to set some variables/parameters that were in the edit controller in the create controller also. One of those happened to be @user.
I added the following to def user.
@users = User.uniq.pluck(:name)
@complaint = Complaint.find(params[:id])
@user = User.find(@complaint.user_id) # this points the complaint to the appropriate user record.
@existing_sent_to = DocumentHistory.where(:complaint_id => $document_assign)
$existing_sent_to = DocumentHistory.where(:complaint_id => $document_assign)
@attachments = @complaint.attachments
@user = User.find(@complaint.user_id) # this points the complaint to the appropriate user record.
# Grab the current complaint id, put it in $current_record_id. Then use that in the where clause.
# to show the document histories for the current record
$current_record_id = @complaint.id
$document_histories = DocumentHistory.where(["complaint_id like ?", "%#{$current_record_id}%"])
That seems to have resolved the issue. I still need to test this but I think it is working ok. I am no longer getting the error screen on a failed validation. I am simply getting the edit form with the rails validation messages.