我的用户 Controller 中有一个名为 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
这是我的用户 Controller
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!
关于javascript - 无法让我的文本值在我的查询中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29236518/