2016-03-04 72 views
1

我有一個調用投訴表單的視圖。它也恰好在同一頁上有部分內容。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> 

回答

1

好像這裏有幾個潛在的問題,沒有更多的代碼,很難說明驗證如何發揮作用。

讓我們先看看你的服務器日誌。它似乎告訴我們的是:你的代碼試圖在nil上調用.name - 這是不好的。

這條線,是它borking上線:
app/views/complaints/_form.html.erb:43:in block in_app_views_complaints__form_html_erb__591273067_73900308'

如果我們把你的模板代碼來看看第43行,它看起來像你正在嘗試調用.name@user。好的,那意味着,@user在那一點上是零。奇怪的。爲什麼會這樣?

因此,您的第一筆業務是確定爲什麼@user此時無效?你有沒有機會通過你的控制器代碼,並確保你將它發送到視圖之前設置@user變量?

+0

我想我看到發生了什麼。我在周圍的原始文章中添加了周圍的代碼。當我正在進行編輯/更新時,我得到錯誤的代碼不應該執行,但是,當我驗證失敗時,它正在執行。我正在查看編輯和更新控制器。 –

0

@queserasera讓我指出了正確的方向。看來,在驗證失敗的情況下,我需要設置一些變量/那名在創建控制器還編輯控制器參數。其中一個碰巧是@user。

我向def用戶添加了以下內容。

@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}%"]) 

這似乎解決了這個問題。我仍然需要測試這一點,但我認爲這是工作確定。我不再在驗證失敗時獲取錯誤屏幕。我只是通過rails驗證消息獲取編輯表單。

相關問題