我有一个点击事件触发了对rails应用中记录的更新。服务器错误是会议无法识别字符串,因此我将id转换为int。然后我遇到了路障。我尝试了两种不同的方式,服务器错误都低于
$('.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);
}
});
})
});
});
当我控制log 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调用中,它确实声明了meeting:id,尝试修复它,并且它收到此错误
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'
为什么这不起作用?特别是考虑到我使用网址将其直接发送到控制器,所以它不应该只是通过网址传递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"):
在rails脚手架自动创建的更新方法中,它有一行在更新后重定向
format.html { redirect_to home_url, notice: 'Your lex is set! Click Plan Meeting to plan some conversation topics.' }
希望它可以帮助别人:)
答案 0 :(得分:2)
您的错误似乎是由控制器中的导轨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
看起来像:
meeting
Rails正在寻找所需的{"cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"khkj", "id"=>"76"}
密钥。在你的第一个例子中,传入的参数是:
{"meeting" => {"cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"khkj", "id"=>"76"}}
Rails期待像这样的params哈希:
ajax
修改$.ajax({
type: "PUT",
url: '/meetings/'+id,
data: {
meeting: {
id: id,
cancel_user_id: userId,
status: 'Cancelled',
notes: cancelNotes
}
},...
请求应该注意:
meeting
第二次错误
当您实际在哈希中传递params
时Parameters: {"meeting"=>"76", "cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"kjl", "id"=>"76"}
彻底通过:
meeting
Rails实际上会看到permit
密钥,因为它现在存在并在其上调用params[:meeting]
。但是,"76"
会返回字符串permit
。 Rails希望在hash
上拨打NoMethodError (undefined method `permit' for "76":String)
。因此,这就是为什么你看到第二个错误。
{{1}}