可以't使我的文本值在查询中起作用

Can't get my text value to work in my query

本文关键字:查询 起作用 文本 我的 可以      更新时间:2023-09-26

我的用户控制器中有一个名为populate_form的方法,它的作用是自动从我的另一个表中获取数据,该表将与他们在emp_id文本字段中键入的内容相匹配。。例如,他们键入SMITHJ,这个值应该与我的视觉模型进行比较,如果它找到匹配,它将把他们的名字返回到emp_First_name文本字段emp_First_name=John中。

Clarification 
 visual model has a column called id
 user model has a column called emp_id 
 these are both the same so if the users emp_id = SMITHJ it will be visual id = SMITHJ

由于某种原因,我无法获得在Emp_id文本字段中键入的数据来查看我的视觉模型。

它不断地返回这个。。。。。为什么它使它无效?

Started GET "/user/populate_form&emp_id=SMITHJ" for 127.0.0.1 at 2015-03-23 19:43:43 -0400
Processing by UserController#show as JSON
Parameters: {"id"=>"populate_form&emp_id=SMITHJ"}
Visual Load (2.2ms)  SELECT  "EMPLOYEE".* FROM "EMPLOYEE"  WHERE "EMPLOYEE"."ID" IS NULL AND ROWNUM <= 1

这是我的用户控制器

class UserController < ApplicationController

 def populate_form
   @visual = Visual.find_by_id(params[:emp_id])
   @emp_first_name = @visual.first_name
     render :json => {
        :emp_first_name => @emp_first_name
     }
   end
 end 

 def show
   @visual = Visual.find_by_id(params[:emp_id])
   @emp_first_name = @visual.first_name
     render :json => {
        :emp_first_name => @emp_first_name
     }
   end
 end 

这是我的应用程序js

$(document).ready(function(){
  $('#emp_id').change(function() {
        var url = "/user/populate_form&emp_id="+$(this).val();
        $.getJSON(url, function(data) {
          if(!(data.emp_first_name === undefined))
          $('#emp_first_name').val(data.emp_first_name);
        });
      }
    );
  });

这是我的观点。。。

<div class='row form-group'>
   <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
    <%= f.text_field :emp_id, tabindex: 1, id: 'emp_id', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
  </div>
</div>
 <div class='row form-group'>
  <div class='col-xs-8 col-xs-offset-2 col-sm-6 col-sm-offset-3 col-md-4 col-md-offset-4 col-lg-2 col-lg-offset-5 text-right'>
    <%= f.text_field :emp_first_name, tabindex: 1, id: 'emp_first_name', autofocus: true, placeholder: t( 'login_label' ), class: 'form-control' %>
   </div>
 </div>

如@NekoNova建议----

将url更改为/user/populate_form?emp_id=SMITHJ,否则/user之后的所有内容都将被视为您的id

$(document).ready(function(){
   $('#emp_id').change(function() {
    var url = "/user/populate_form&emp_id?"+$(this).val();
    $.getJSON(url, function(data) {
      if(!(data.emp_first_name === undefined))
      $('#emp_first_name').val(data.emp_first_name);
    });
  }
);
});

我一做到这一点就奏效了!再次感谢@NekoNova!