2016-04-13 222 views
-1

對我的表單字段驗證有些麻煩。我有兩個文本字段需要驗證才能保留到下一頁。驗證運行正確,但驗證消息顯示類似導軌錯誤。字段驗證Ruby on Rails

enter image description here

但我想這個錯誤,如下面enter image description here

任何人知道爲什麼它顯示像軌誤差。

型號:

class Assignment < ActiveRecord::Base 
    include Workflow 

    belongs_to :folder 
    belongs_to :employee 

    after_initialize :init_start_dateenter code here 

    validates_presence_of :folder_id, :employee_id 
end 

控制器:

class AssignmentsController < ApplicationController 
    def create 
    @assignment = Assignment.new(assignment_params) 

    respond_to do |format| 
     if @assignment.save! 
     format.html { redirect_to @assignment, notice: 'Assignment was successfully created.' } 
     format.json { render :show, status: :created, location: @assignment } 
     @assignment.folder.update({status: 'assigned'}) 
     else 
     format.html { render :new } 
     format.json { render json: @assignment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

end 

我有兩個形式驗證領域。在這種形式中,驗證錯誤顯示正確。

回答

2

您需要刪除從保存!,因爲這將引發該線路上的錯誤,如果你省略了!它只會返回一個布爾(然後渲染錯誤或成功。

所以

class AssignmentsController < ApplicationController 
    def create 
    @assignment = Assignment.new(assignment_params) 

    respond_to do |format| 
     if @assignment.save 
     format.html { redirect_to @assignment, notice: 'Assignment was successfully created.' } 
     format.json { render :show, status: :created, location: @assignment } 
     @assignment.folder.update({status: 'assigned'}) 
     else 
     format.html { render :new } 
     format.json { render json: @assignment.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

end 
+0

謝謝JamesWatling :)這是我的錯誤 – ArpithaGeorge