问题是在我的 Rails 应用程序中,我的用户 Controller 中有一个方法可以自动填充。这会在新用户注册时填写一个表格。当他们填写我的 emp_id 文本框时,它会查看我的员工表,如果 ID 与他们输入的 emp_id 匹配,我将从该表中提取此数据。自动填充 emp_first_name 和 emp_last_name。当数据返回有数据时,我能够弹出一条消息,但是,我一直在思考如何在该特定 emp_id 不存在数据时显示消息...
这是我的 Controller
class UserController < ApplicationController
def populate_form
@visual = Visual.find_by_id(params[:emp_id])
if @visual.nil?
respond_to do |format|
format.json { render json: @user, status: :unprocessable_entity, flash[:error] => "Error No ID found." }
#This didn't work no message displays..
end
else
@emp_first_name = @visual.first_name
@emp_last_name = @visual.last_name
render :json => {
:emp_first_name => @emp_first_name,
:emp_last_name => @emp_last_name
}
end
end
这是我的观点..
<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( 'emp_first' ), 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_last_name, tabindex: 1, id: 'emp_last_name', autofocus: true, placeholder: t( 'emp_last' ), class: 'form-control' %>
</div>
这是我的 app.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);
if(!(data.emp_last_name === undefined))
$('#emp_last_name').val(data.emp_last_name);
if('#emp_first_name' === null) {
alert('Fail employee ID wasn't found please try again with a valid employee ID.');
} else {
alert('Your employee ID has been found please fill in the email and password fields then click submit to register.');
}
});}
);
});
最佳答案
.您可以使用不同的ajax callbacks检查 json 响应是否失败,然后显示错误消息。您可以使用空白占位符来显示警报消息。例如:-
在 Controller 中使用它来显示 json 错误
class UserController < ApplicationController
def populate_form
@visual = Visual.find_by_id(params[:emp_id])
if @visual.present?
respond_to do |format|
@emp_first_name = @visual.first_name
@emp_last_name = @visual.last_name
render :json => {
:emp_first_name => @emp_first_name,
:emp_last_name => @emp_last_name
}
else
format.js { render :json => "id not present", :status => 400 }
format.html { render :json => "id not present" , :status => 400 }
end
end
end
查看文件,带占位符
<div id="show_alert" style="display:none;"></div>
js代码,显示警报
$('#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);
if(!(data.emp_last_name === undefined))
$('#emp_last_name').val(data.emp_last_name);
}).done(function (response) {
if (response.success == 'success') {
//alert('success');
$("#show_alert").html("<span class='text-success marginl10' ><b>Your employee ID has been found please fill in the email and password fields then click submit to register..</b></span>")
} else {
//alert('fail');
$("#show_alert").html("<span class='text-danger marginl10' ><b>Fail employee ID wasn't found please try again with a valid employee ID.</b></span>").show();
}
});
}
);
});
关于javascript - 在 JavaScript 中为我的 Rails 应用程序显示弹出消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29389869/