2016-06-08 42 views
0

我正在使用Rails 4.2.3和「:remote => true」屬性來遠程提交表單。但是,我無法顯示回來的驗證錯誤。在我的控制器我有如何從遠程提交的表單返回JSON格式的驗證錯誤?

def create 
    @my_object = MyObject.new(my_object_params) 
    @current_user = User.find(session["user_id"]) 
    @my_object.user = @current_user 
    @date = Date.strptime(my_object_params[:day], "%m/%d/%Y") 
    @my_object.day = @date 
    respond_to do |format| 
     if @my_object.save 
     flash[:notice] = 'Saved successfully' 
     format.html { redirect_to controller: "users", action: "index", notice: 'Saved successfully.' } 
     format.js { render js: "window.location='/users'" } 
     else 
     format.html { render action: "index" } 
     format.json { render json: @my_object.errors, status: :unprocessable_entity } 
     format.js { render json: { error: @my_object.errors, success: false }, content_type: 'application/json' } 
     end 
    end 
    end 

而且在我的咖啡劇本,我有

$(document).ready (data) -> 
    $("#add_form").bind "ajax:error", (xhr, status, error) -> 
    console.log(xhr) 
    console.log(status) 
    console.log(error) 
    render_form_errors(xhr, error) 
    #Give an alert message 
    $("#add_form").bind "ajax:before", (evt, data, status, xhr) -> 
    hrs = $('#my_object_hour').val(); 
    min = $('#my_object_minute').val(); 
    sec = $('#my_object_second').val(); 
    $('#my_object_my_object_times_attributes_0_time_in_ms').val((hrs * 60 * 60 + min * 60 + sec) * 1000); 

但沒有這三個領域,「XHR,狀態,錯誤」包含了JSON錯誤。 console.log語句的輸出是

j…y.Event {type: "ajax:error", timeStamp: 1465418664628, jQuery112103593207809583596: true, isTrigger: 3, namespace: ""…} 
users.self-a27ba2c….js?body=1:13 Object {readyState: 4, responseText: "ArgumentError in MyObjectsController#create↵↵invalid d…VER_PROTOCOL: "HTTP/1.1"↵↵Response headers↵None↵↵", status: 500, statusText: "Internal Server Error"} 
users.self-a27ba2c….js?body=1:14 error 

如何從控制器傳回這些數據?

謝謝, -

編輯:爲了應對答案,我在我的方式控制指定的編輯和添加到了我的咖啡腳本...

$(document).ready (data) -> 
    $("#add_form").bind "ajax:failure", (e, xhr, status, error) -> 
    console.log('error'); 
    console.log(e); 
    console.log(xhr); 
    console.log(status); 
    console.log(error); 
    render_form_errors(xhr, error) 
    $("#add_form").bind "ajax:error", (xhr, status, error) -> 
    console.log(xhr) 
    console.log(status) 
    console.log(error) 
    render_form_errors(xhr, error) 
    #Give an alert message 
    $("#add_form").bind "ajax:before", (evt, data, status, xhr) -> 
    hrs = $('#my_object_hour').val(); 
    min = $('#my_object_minute').val(); 
    sec = $('#my_object_second').val(); 
    hrs_in_ms = hrs * 60 * 60 * 1000 
    min_in_ms = min * 60 * 1000 
    sec_in_ms = sec * 1000 
    ms = hrs_in_ms + min_in_ms + sec_in_ms 
    alert(ms) 
    $('#my_object_my_object_times_attributes_0_time_in_ms').val(ms); 

但在提交我的表單有錯誤,以上都沒有被調用。我知道有錯誤是因爲我看到(通過調試)被調用的「if @ my_object.save」的「else」子句。

+0

這是一個500內部服務器錯誤,因爲'ArgumentError MyObjectsController#create' –

回答

1

首先你需要修復500錯誤,我沒有拿你的例子,但這是一個粗略的例子。

bugs.js

$(document).ready(function() { 
    console.log('hiii'); 
    $('#new_bug') 

    .on('ajax:success', function(e, data, status, xhr) { 
    console.log('success'); 
    console.log(e); 
    console.log(data); 
    console.log(status); 
    console.log(xhr); 
    }) 

    .on('ajax:error', function(e, xhr, status, error) { 
    console.log('error'); 
    console.log(e); 
    console.log(xhr); 
    console.log(status); 
    console.log(error); 
    }) 
}) 
在控制器

,對於例如我的控制器是 bugs_controller.js

def create 
    @bug = Bug.new(bug_params) 

    respond_to do |format| 
     if @bug.save 
     format.html { redirect_to @bug, notice: 'Bug was successfully created.' } 
     format.json { render :show, status: :created, location: @bug } 
     format.js { render json: { success: true, bug: @bug.to_json }, content_type: 'application/json' } 
     else 
     format.html { render :new } 
     format.json { render json: @bug.errors, status: :unprocessable_entity } 
     format.js { render json: { error: @bug.errors, success: false }, content_type: 'application/json' } 
     end 
    end 
    end 

如果你想ajax:error回調您else塊,你將需要提供不同的狀態代碼像400,500。例如 format.js { render json: { error: @bug.errors, success: false }, content_type: 'application/json', status: 500 },雖然我不會真的推薦這個,因爲這不是正確的做法。您的ajax:success將處理驗證錯誤,同樣因爲我們正在通過success: false,我們可以檢查js中是否存在驗證錯誤。

+0

嗨,我編輯了我的quesiton以包含您的代碼(我用咖啡腳本,我認爲您已經嚴格了Javascript),但「ajax :失敗「方法不會在提交帶有驗證錯誤的表單時被調用。 – Dave

+0

您是否在您的其他區塊中添加了format.js – Saad

+0

我忘記在該問題中包含該區塊,但我已將其編輯。有驗證錯誤時會調用什麼事件?是ajax:失敗? – Dave