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