2016-10-06 77 views
1

我有一個單擊事件觸發更新的軌道應用程序中的記錄。服務器錯誤是會議無法識別字符串,所以我把id變成了一個int。然後我碰到了一個路障。我嘗試兩種不同的方式,這兩種服務器錯誤都低於阿賈克斯把請求不接受在參數中的ID

$('.cancelMeeting').click(function(e) { 
e.preventDefault(); 

console.log(this.href); 
var fullUrl = this.href; 
var hrefLength = (this.href).length; 
var idString = fullUrl.substring(hrefLength-2); 
var id = parseInt(idString); 
console.log(typeof id); 
var source = $(".cancelMeetingPrompt").html(); 
var compiled = Handlebars.compile(source); 
$('.meetingHolder').append(compiled()); 

$('.confirmCancelMeeting').click(function(e) { 
    e.preventDefault(); 
    var cancelNotes = $('.cancelNotes').val(); 
    $.get('/set_user', function(result) { 
      var userId = result.id 
      console.log(userId); 
      console.log(id); 

      $.ajax ({ 
       type: "PUT", 
       url: '/meetings/'+id, 
       data: { 
        meeting: id, **only in second error log below** 
        cancel_user_id: userId, 
        status: 'Cancelled', 
        notes: cancelNotes 
       }, 
       datatype: 'json', 
       success: function(result) { 
        console.log(result); 
       }, 
       error: function(result) { 
        console.log(typeof id); 
        console.log(result); 
       } 
      }); 
    }) 
}); 



}); 

當我控制檯日誌的typeof ID,它說號碼。但是當我運行它時,會給出下面的錯誤。

Started PUT "/meetings/76" for 127.0.0.1 at 2016-10-06 19:38:00 -0400 
Processing by MeetingsController#update as */* 
    Parameters: {"cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"khkj", "id"=>"76"} 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 
    Meeting Load (0.2ms) SELECT "meetings".* FROM "meetings" WHERE "meetings"."id" = ? LIMIT 1 [["id", 76]] 
Completed 400 Bad Request in 6ms (ActiveRecord: 0.5ms) 

ActionController::ParameterMissing (param is missing or the value is empty: meeting): 
    app/controllers/meetings_controller.rb:95:in `meeting_params' 
    app/controllers/meetings_controller.rb:61:in `block in update' 
    app/controllers/meetings_controller.rb:60:in `update' 

在Ajax調用我上面粘貼,它宣佈會議:身份證,試圖補救,並收到此錯誤

Started PUT "/meetings/76" for 127.0.0.1 at 2016-10-06 19:41:24 -0400 
Processing by MeetingsController#update as */* 
    Parameters: {"meeting"=>"76", "cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"kjl", "id"=>"76"} 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 
    Meeting Load (0.1ms) SELECT "meetings".* FROM "meetings" WHERE "meetings"."id" = ? LIMIT 1 [["id", 76]] 
Completed 500 Internal Server Error in 8ms (ActiveRecord: 0.4ms) 

NoMethodError (undefined method `permit' for "76":String): 
    app/controllers/meetings_controller.rb:95:in `meeting_params' 
    app/controllers/meetings_controller.rb:61:in `block in update' 
    app/controllers/meetings_controller.rb:60:in `update' 

爲什麼這個不行?特別是考慮到我使用的URL直接發送到控制器,所以不應該只是拉與URL傳遞的ID?

任何線索,爲什麼這是行不通的讚賞。謝謝!

******* UPDATE *******

多虧了下面的答案,我能夠正確地傳遞參數,可以但另一個錯誤出現。

Started PUT "/meetings/84" for 127.0.0.1 at 2016-10-06 21:32:46 -0400 
Processing by MeetingsController#update as */* 
    Parameters: {"meeting"=>{"cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"gsdf"}, "id"=>"84"} 
    User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT 1 [["id", 1]] 
    Meeting Load (0.2ms) SELECT "meetings".* FROM "meetings" WHERE "meetings"."id" = ? LIMIT 1 [["id", 84]] 
    (0.1ms) begin transaction 
    SQL (0.6ms) UPDATE "meetings" SET "notes" = ?, "updated_at" = ? WHERE "meetings"."id" = ? [["notes", "gsdf"], ["updated_at", "2016-10-07 01:32:46.982349"], ["id", 84]] 
    (1.5ms) commit transaction 
Redirected to http://localhost:3000/profile/home 
Completed 302 Found in 14ms (ActiveRecord: 2.7ms) 


Started PUT "/profile/home" for 127.0.0.1 at 2016-10-06 21:32:46 -0400 

ActionController::RoutingError (No route matches [PUT] "/profile/home"): 

在Update方法中軌自動腳手架造成的,它有被更新後重定向線

format.html { redirect_to home_url, notice: 'Your lex is set! Click Plan Meeting to plan some conversation topics.' } 

希望這可以幫助別人:)

+0

你可以添加你的控制器代碼嗎?特別是許可證方法。 – opticon

回答

2

好像你的錯誤造成通過您的控制器中的導軌strong params,如以下行所示:app/controllers/meetings_controller.rb:95:in 'meeting_params'

(param is missing or the value is empty: meeting)表示您的meeting_params方法缺少必需的密鑰。所以,如果你meeting_params看起來像:

def meeting_params 
    params.require(:meeting).permit(....) 
end 

Rails正在尋找所需的meeting關鍵。在你的第一個例子中,在通過了PARAMS是:

{"cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"khkj", "id"=>"76"} 

Rails的期待params散列,像這樣:

{"meeting" => {"cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"khkj", "id"=>"76"}} 

修改您的ajax請求應該照顧它:

$.ajax({ 
     type: "PUT", 
     url: '/meetings/'+id, 
     data: { 
      meeting: { 
      id: id, 
      cancel_user_id: userId, 
      status: 'Cancelled', 
      notes: cancelNotes 
      } 
     },... 

第二個錯誤

當你真正在散列通meetingparams來徹底爲:

Parameters: {"meeting"=>"76", "cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"kjl", "id"=>"76"} 

Rails的實際看到的meeting關鍵,因爲現在它存在和它調用permit。但是,params[:meeting]返回字符串"76"。 Rails希望在hash上致電permit。因此,這就是爲什麼你看到第二個錯誤。

NoMethodError (undefined method `permit' for "76":String) 
+0

是的!謝謝 - 我真的很感激詳細的解釋.. – gwalshington