gwalshington gwalshington - 1 month ago 5
Ajax Question

ajax put request not accepting id in params

I have a click event triggering an update to a record within a rails app. The server error was that meeting doesn't recognize a string, so I turned the id into an int. Then I ran into a road block. I tried 2 different ways, both server errors are below

$('.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);
}
});
})
});



});


When I console log typeof id, it says number. But when I run it, the error below is given.

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'


In the ajax call I pasted above, it does declare meeting :id, to try and remedy that, and it receives this error

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'


Why does this not work? Especially considering I'm using the url to send it straight to the controller, so shouldn't it just pull the id passed with the url?

Any clues as to why this isn't working is appreciated. Thanks!

Answer

It seems like your error is caused by rails strong params in your controller, as indicated by this line: app/controllers/meetings_controller.rb:95:in 'meeting_params'.

(param is missing or the value is empty: meeting) is indicating that your meeting_params method is missing a required key. So, if your meeting_params looks something like:

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

Rails is looking for the the required meeting key. In your first example, the params passed in were:

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

Rails was expecting a params hash like this:

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

Modifying your ajax request should take care of it:

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

Second Error

When you actually pass meeting in the hash your params come thorough as:

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

Rails actually sees the meeting key, because now it is present and calls permit on it. However, params[:meeting] returns the string "76". Rails expects to call permit on a hash. Thus, thats why your seeing the second error.

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