如果某个东西不是'未找到

How can I display a pop up message if something isn't found?

本文关键字:如果      更新时间:2023-09-26

问题是,我有一个查询,在输入内容后会查看我的emp_id文本框,它会搜索与该emp_id相关的信息,该信息在我的另一个表中搜索其调用的id。如果emp_id与我的视觉模型中的id匹配,它会填充它们的名字和姓氏。我想知道,如果在我的视觉模型中没有emp_id与id匹配,我如何让它显示错误。。。。

这是我的视觉模型

 class Visual < ActiveRecord::Base
 establish_connection :vtest
 self.table_name = 'employee'

 Visual.inheritance_column = 'inheritance_type'
 belongs_to :user
 def emp_matches(x)
   (x.id.to_i == self.id.to_i ? true : false)
 end
end

这是我的用户控制器,它有我的填充表单方法

 class UserController < ApplicationController

   def populate_form
     @visual = Visual.find_by_id(params[:emp_id])
     if @emp_matches == 'False'
       respond_to do |format|
         format.json { render json: @user.errors, status: :unprocessable_entity, flash[:error] => "Error No ID found." }
     end
   else
     @visual = Visual.find_by_id(params[:emp_id])
     @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

这是我的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.last_name === undefined))
           $('#emp_last_name').val(data.emp_last_name);
         });
       }
     );
   });

我的桌子

视觉模型

      Table is called Employee 
        ID
        first_name
        last_name

用户模型

      Table is called User 

        emp_id
        emp_first_name
        emp_last_name        

您可以在视图中使用通知。

看看以下内容:这里是

这可能会有所帮助。