我有一个点击事件,触发了Rails应用程序中记录的更新。服务器错误是会议无法识别字符串,因此我将ID转换为int。然后我遇到了一个路障。我尝试了2种不同的方法,下面两个服务器错误
$('.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调用中,它确实声明了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'
为什么这不起作用?特别是考虑到我正在使用url直接将其发送到控制器,那么它不应该只是拉取随url传递的id吗?
任何有关为什么这不起作用的线索表示赞赏。谢谢!
*******更新*******
由于以下答案,我能够正确传递参数,但是又出现了另一个错误。
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 scaffolding自动创建的更新方法中,它的一行在更新后进行重定向
format.html { redirect_to home_url, notice: 'Your lex is set! Click Plan Meeting to plan some conversation topics.' }
希望它可以帮助其他人:)
最佳答案
看来您的错误是由控制器中的导轨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
密钥。在第一个示例中,传入的参数为:{"cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"khkj", "id"=>"76"}
Rails期望这样的参数哈希:
{"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
}
},...
第二次错误
当您在散列中实际传递
meeting
时,您的params
可以理解为:Parameters: {"meeting"=>"76", "cancel_user_id"=>"1", "status"=>"Cancelled", "notes"=>"kjl", "id"=>"76"}
Rails实际上看到了
meeting
键,因为现在它已存在并在其上调用permit
。但是,params[:meeting]
返回字符串"76"
。 Rails希望在permit
上调用hash
。因此,这就是为什么您看到第二个错误。NoMethodError (undefined method `permit' for "76":String)