在url中使用时,id_number变量返回null

时间:2014-01-21 14:09:06

标签: javascript jquery ruby-on-rails

index.html.erb

<div id="id_number">-</div>

<script type="text/javascript">
  $(document).ready(function() {

    $('#commendations_click').click(function(evt) {
      evt.preventDefault();
      window.location = "<%=j commendations_path(format: 'pdf') %>" 
        + "?idnumber=" + $('#id_number').html() 
        + "&amp;employee_movement_id=" + $('#employee_movements_select').val()
    });

    $('#empname').autocomplete({ 
      source: $('#empname').data('autocomplete-source'), 
      select: function( event, ui ) {
        $("#empname").val(ui.item.last_name + ', ' + ui.item.first_name + ' ' + ui.item.middle_name);
        $("#name").html(ui.item.last_name + ', ' + ui.item.first_name + ' ' + ui.item.middle_name);
        $("#id_number").html(ui.item.id_number);

        $.ajax({
          url: "<%= update_employee_movements_path %>",
          data: { id_number : ui.item.id_number },
          dataType: "script"
        });

        return false;
      }
    })
    .data( "ui-autocomplete" )._renderItem = function( ul, item ) {
      return $( "<li>" )
        .append( "<a>Name: " + item.last_name + ', ' + item.first_name + ' ' + item.middle_name + "<br>ID: " + item.id_number + "</a>" )
        .appendTo( ul );
    };

  });
</script>

reports_controller.rb

def commendations
  emdates = EmployeeMovement.last_2_dates_obtained(params[:employee_movement_id])
  date_from = emdates[0].date_obtained
  date_to   = emdates.length == 1 ? nil : emdates[1].date_obtained
  emp       = Employee.find_by_id_number(params[:id_number])

  ...more code below...
end

请看看最后一行。这里,params [:id_number]将返回nil。但是,如果我在URL中将id_number更改为idnumber,并调用params [:idnumber],我将获得相关值。

为什么从我的表彰行动中访问时,id_number会返回nil?这是一个约定吗?在jquery侧或轨道侧的约定?

1 个答案:

答案 0 :(得分:1)

HTML代码中的某处,元素的ID为idnumber而不是id_number。它不是惯例,而是代码中的拼写错误。