Rails正在发送我没有制作的参数

时间:2013-11-22 01:29:34

标签: jquery ruby-on-rails

我正在创建一个创建聊天室页面。

如果用户输入数据并按下“创建”按钮,则会调用此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;
                }
            });
        });

在这里,获取所有数据,并以json格式制作,发送给控制器。

但是,如果我检查那里的参数,它会发送我没有制作的“房间”。

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"}]}}

有什么好的理由和解决方案吗?

1 个答案:

答案 0 :(得分:0)

我已经看到了类似的东西,其中params被克隆在一个名为与我的控制器动作相同的节点下,在你的情况下,它看起来是在你的控制器命名的节点下 - 我的猜测是当rails解析时出现的奇怪现象传入json进入params ruby​​ hash

与此无关,是否有理由将json构建为字符串?不应该做以下工作:

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 ...

如果可能,还要考虑使用命名约定,现在您有以下内容:

  • 以下划线('end_time','start_time')
  • 降低
  • camel
  • Pascal('日期')
  • 全部较低,没有下划线('roomvalue','attendeelist')
它会导致混乱,我会选择一种风格 - 无所谓。

  

通常在ruby中,对变量使用lower_case_with_under_score,对类名使用PascalCase。 Javascript通常是camelCase。