2017-05-07 5 views
2

Railsでフォームを構築しています。私は、Ajaxによって、それを提出したいので、私は私のコントローラでremote: trueイベントajaxの応答からデータを取得する方法:Rails 5.1のエラー

<%= form_for @post, html: { multipart: true }, remote: true do |f| %> 
    ... 
<%= end %> 

をsetted:ビューのフォームで

def create 
    @post = Post.new(post_param) 

    respond_to do |format| 
     if @post.save 
      format.html { redirect_to @post, notice: 'Post was successfully created.' } 
      format.json { render :show, status: :created, location: @post } 
     else 
      format.html { render json: @post.errors, status: 400 } 
      format.json { render json: @post.errors, status: 400 } 
     end 
    end 
end 

$('#new_post').on('ajax:success', function(data) { 
    console.log(data); 
}).on('ajax:error', function(evt, xhr, status, error) { 
    console.log(xhr); 
}); 

しかし、イベントの 'AJAX:エラー'、投稿の検証エラーに関するデータを取得するために解析することはできません。すべてのパラメータ 'xhr'、 'status'、 'error'はどちらも未定義です。

'ajax:error'の応答を解析して、検証エラーに関するデータを取得するにはどうすればよいですか?

答えて

関連する問題