简体   繁体   English

Rails正在发送我没有做过的参数

[英]Rails is sending params that i didn't make

I'm making a create chat room page. 我正在创建聊天室页面。

If the user enter data and press create button it calls this jquery function. 如果用户输入数据并按下创建按钮,它将调用此jquery函数。

$(".create_room_btn").click(function(){
            var token = $("#authenticity_token").val();
            var appointmentName = $("#appointmentName").val();
            var date = $("#Date").val();
            var start_time = $("#start_time").val();
            var end_time = $("#end_time").val();
            var roomType = $("#roomType").val();
            var numberOfParticipants = $("#numberOfParticipants").val();
            var appointmentDescription = $("#appointmentDescription").val();
            var roomValue = '"roomvalue": {';
            //roomValue += '"token": "' + token + '",';
            roomValue += '"appointmentName": "' + appointmentName + '",';
            roomValue += '"Date": "' + date+ '",';
            roomValue += '"start_time": "' + start_time + '",';
            roomValue += '"end_time": "' + end_time + '",';
            roomValue += '"roomType": "' + roomType + '",';
            roomValue += '"numberOfParticipants": "' + numberOfParticipants + '",';
            roomValue += '"appointmentDescription": "' + appointmentDescription + '"';
            roomValue += '}';


            var attendeeList = '"attenddeelist": [';
            $("#invited_table tr").find('td').each(function(a,b){
                if(b.cellIndex==0){
                    attendeeList += '{ "id": "' + b.innerHTML+'"';
                }else if(b.cellIndex==1){
                    attendeeList += ',"firstname": "' + b.innerHTML+'"';
                }else if(b.cellIndex==2){
                    attendeeList += ',"lastname": "' + b.innerHTML+'"';
                }else if(b.cellIndex==3){
                    attendeeList += ',"email": "' + b.innerHTML+'"},';
                }
                //console.log(b.innerHTML);
            })
            // delete the last "," to make a json format
            attendeeList = attendeeList.substring(0,attendeeList.lastIndexOf(","));
            attendeeList += ']';

            //var params = '{' + '"authenticity_token":"'+token+"","+ roomValue + ',' + attendeeList + '}';

            var params = "{"+roomValue + "," + attendeeList + "}";
            //console.log(params);


            $.ajax({
                type: "post",
                contentType : "application/json; charset=utf-8",
                beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', $('meta[name="csrf-token"]').attr('content'))},
                url : "/room/createroom",
                dataType : "json",
                data :  params,
                success : function() {
                    return false;
                },
                error: function(){
                    return false;
                }
            });
        });

in here, gets all the data, and made on json format, send it to the controller. 在这里,获取所有数据,并以json格式制作,然后将其发送到控制器。

but If i check the params in there, it is sending "room" that i didn't make. 但是如果我检查那里的参数,它会发送我没有做的“房间”。

Parameters: {"roomvalue"=>{"appointmentName"=>"title", "Date"=>"date", "start_time"=>"start", "end_time"=>"end", "roo
mType"=>"1", "numberOfParticipants"=>"max", "appointmentDescription"=>"des"}, "attenddeelist"=>[{"id"=>"22", "firstname
"=>"Michimasa", "lastname"=>"Ueematsu", "email"=>"Michimasa.Uematsu@access-company.com"}, {"id"=>"16", "firstname"=>"Ha
eseok", "lastname"=>"Maeng", "email"=>"Haeseok.Maeng@access-company.com"}, {"id"=>"0", "firstname"=>"fist", "lastname"=
>"las", "email"=>"email"}], "room"=>{"roomvalue"=>{"appointmentName"=>"title", "Date"=>"date", "start_time"=>"start", "
end_time"=>"end", "roomType"=>"1", "numberOfParticipants"=>"max", "appointmentDescription"=>"des"}, "attenddeelist"=>[{
"id"=>"22", "firstname"=>"Michimasa", "lastname"=>"Ueematsu", "email"=>"Michimasa.Uematsu@access-company.com"}, {"id"=>
"16", "firstname"=>"Haeseok", "lastname"=>"Maeng", "email"=>"Haeseok.Maeng@access-company.com"}, {"id"=>"0", "firstname
"=>"fist", "lastname"=>"las", "email"=>"email"}]}}

Any good reason and solution? 有什么好的理由和解决方案吗?

I have seen something similar, where the params are cloned under a node named the same as my controller action, in your case it looks to be under a node named after your controller - my guess is something weird happens when rails parses the incoming json into the params ruby hash 我见过类似的东西,参数被克隆到与我的控制器动作相同的节点下,在您的情况下,它看起来像是在以控制器命名的节点下-我猜这是奇怪的,当Rails将传入的json解析为params红宝石哈希

unrelated to this, is there a reason you are building up json as a string? 与此无关,是否有理由将json建立为字符串? shouldn't the following work: 不应该做以下工作:

var attendeeList = [];

$("#invited_table tr").each(function (index, element) {
  var attendee = {};

  element.find('td').each(function (a, b) {
    if(b.cellIndex==0){
      attendee['id'] = b.innerHTML;
    }else if(b.cellIndex==1){
      attendee['firstname'] = b.innerHTML;
    }else if(b.cellIndex==2){
      attendee['lastname'] = b.innerHTML;
    }else if(b.cellIndex==3){
      attendee['email'] = b.innerHTML;
    }
  });

  attendeeList.push(attendee);
});

var params = {
  roomvalue: {
    appointmentName: $("#appointmentName").val(),
    "Date": $("#Date").val(),
    start_time: $("#start_time").val(),
    end_time: $("#end_time").val(),
    roomType: $("#roomType").val(),
    numberOfParticipants: $("#numberOfParticipants").val(),
    appointmentDescription: $("#appointmentDescription").val(),
    attendeelist: attendeeList
  }
};   

// make ajax call ...

Also consider using a naming convention if possible, right now you have the following: 如果可能的话,还考虑使用命名约定,现在您具有以下条件:

  • lower with underscore ('end_time', 'start_time') 下划线('end_time','start_time')降低
  • camel 骆驼
  • Pascal ('Date') 帕斯卡(日期)
  • all lower with no underscores ('roomvalue', 'attendeelist') 较低的所有字母都没有下划线(“ roomvalue”,“ attendeelist”)

its going to lead to confusion, I would pick one style - doesn't matter which. 它会导致混乱,我会选择一种样式-没关系。

Typically in ruby you use lower_case_with_under_score for variables and PascalCase for class names. 通常在ruby中,您使用lower_case_with_under_score作为变量,并使用PascalCase作为类名。 Javascript is usually camelCase. Javascript通常是camelCase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM