2016-11-22 11 views
1

Ajax on Railsを使用して、400というエラーが発生しています。 フォームを送信するときに、Jqueryからパラメータとして送信する文字列があり、文字列を抽出してコントローラで保存できるように、params [:assignee]から取得したいと考えています。 マイコントローラ:Ajaxが悪いリクエスト400 Rails

def create 
    @task = Task.new(task_params) 
    @task.user = current_user 
    username = params.permit[:assignee] 
    @task.assignee = username 

    #set_category 
    respond_to do |format| 
     if @task.save 
     format.html { redirect_to tasks_url, notice: 'Task was successfully created. '+task_params.inspect} 

     #format.html { redirect_to @task, notice: 'Task was successfully created.' } 
     format.json { render :show, status: :created, location: @task } 
     else 
     format.html { render :new } 
     format.json { render json: @task.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

def task_params 
    params.require(:task).permit(:owner, :value, :completed, :category, :date, :assignee) 

end 

そして、これは私のJSです:

$("#new_task").submit(function() { 
    alert("form: "+assignee); 
    //event.preventDefault(); 
$.ajax({ 
    url: "/tasks", 
    type: "POST", 
    data: {assignee}, 
    dataType: "json", 
    success: function(data) { 
     alert('successfully'); 
    }, 
    error: function(xhr, textStatus, error) { 
    alert(xhr.statusText+""+textStatus+""+error); 
    } 
    }); 
}); 

譲受人はjqueryのオートコンプリートフォームで選択したユーザー名です。

select: function(event, ui) { 
    var terms = split(this.value); 
    // remove the current input 
    terms.pop(); 
    // add the selected item 
    terms.push(ui.item.value); 
    // add placeholder to get the comma-and-space at the end 
    terms.push(""); 
    this.value = terms.join(""); 
    assignee=this.value; 
    $('input[name=commit]').prop("disabled",false); 

    return false; 
} 

私のルートは、「タスク/あります保存されたタスクと新しいフォームを作成するフォームが表示されます。 ネットでたくさん検索してみました。どのようにできるのか?本当にありがとうございまし

+0

作成タスクに連想させる「ポストタスク」の私のフォームの値を失うだろうか? – inye

+0

404エラーは、クライアントがルートを見つけることができないことを意味します。 routes.rbを確認してください。 –

+0

申し訳ありませんが、私の間違い!それは400です。コンソール: http:// localhost:3000/tasks/400(Bad Request) – ValeMarz

答えて

3

400不正な要求 - サーバーがまたは原因 見かけクライアントのエラー(例えば、不正なリクエストの構文、大きすぎる サイズ、無効な要求メッセージフレーミング、または欺瞞的にリクエストを処理しませんすることはできません要求ルーティング)。

wiki

変更へajaxコード:それはまた、あなたは、'Content-Type'と(X-CSRF-TOKENオプションを有効ヘッダを追加する必要があり{assignee: assignee} する必要があります有効ではありませんJSONオブジェクトです

$.ajax({ 
    url: "/tasks", 
    type: "POST", 
    dataType: "json", 
    headers: { 
     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'), // Optional 
     'Content-Type': 'application/json' 
    }, 
    data: JSON.stringify({ assignee: assignee }), 
    success: function(data) { 
     alert('successfully'); 
    }, 
    error: function(xhr, textStatus, error) { 
    alert(xhr.statusText+""+textStatus+""+error); 
    } 
}); 

{assignee}

+0

ok console consoleしかし、私はパラメータの譲受人を見ることができません(私は "task_params.inspectに通知して、どのパラメータが渡されたかを調べることができます")ので、警告はエラー "エラー"を表示します。 – ValeMarz

+0

assignee = this.value; 'should 'assignee'が' task'キーの外にあるので 'params.inspect'でparamsをチェックしてください。 –

+0

私はvarの譲受人を先頭に設定しました – ValeMarz

0

解決済み!

$("#new_task").submit(function(event) { 
    alert("form: "+assignee); 
    var value = $('#new_task').find('input[name="task[value]"]').val(); 
event.preventDefault(); 
$.ajax({ 
    url: "/tasks", 
    type: "post", 
    contentType: "application/json", 

    data: JSON.stringify({ assignee: assignee, value: value }), 
    success: function(data) { 
     alert('successfully'); 
    }, 
    error: function(xhr, textStatus, error) { 
    alert(xhr.statusText+" "+textStatus+" "+error); 
    } 
}); 

}); 

event.preventDefault(); - >フォームが2回送信されます。

var value = $('#new_task').find('input[name="task[value]"]').val(); - >これなしで、私は理由#はブラウザのコンソールに何を得る