2017-01-03 2 views
0

私はので、私は、AJAXの応答を取得する必要がありますコントローラのアクションカントcatckのAjaxのレスポンス

def create 
    @place = Place.new(place_params) 

    respond_to do |format| 
    if @place.save  
    format.json { render json: @place, status: :created }  
    end 
end 

とフォーム

 div.col-sm-6 
     h1 Place 
     =form_for Place.new, remote: true do |f|                 
      div 
      = f.label :address, class: 'label_hidden' 
      = f.text_field :address, class: "form-control" 
      div 
      = f.label :title, class: 'label_hidden' 
      = f.text_field :title, class: "form-control" 
      div 
      = f.label :longitude, class: 'label_hidden' 
      = f.text_field :longitude, class: "form-control" 
      div 
      = f.label :latitude, class: 'label_hidden' 
      = f.text_field :latitude, class: "form-control" 
      div 
      = f.submit 'Create', class: "btn btn-default" 

を持っています。私はこのように何かを試しました

$(document).ready -> 
    $("#new_place").on("ajax:success", (e, data, status, xhr) -> 
     console.log xhr.responseText 
    ).on "ajax:error", (e, xhr, status, error) -> 
     console.log "ERROR" 

しかし、それは動作しません。これに関する助けが必要

答えて

1

Rails ujsがjsonの要求を送信し、jsでないようにフォームを設定する必要があります。

= form_for Place.new, data: { remote: true, type: 'json' } do |f|