我的用户控制器中有一个名为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>
答案 0 :(得分:1)
@NekoNova建议----
将网址更改为/ 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!