2015-03-31 17 views
0

问题是我有一个查询,它查看我的emp_id文本框中,一旦输入内容搜索与该emp_id绑定的信息,它在我的另一个表中搜索其被调用的ID。如果emp_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   

回答

0

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

看一看下面:here

这可能会有帮助。

相关问题