Pasha
Pasha

Reputation: 33

How TO Get the email when we seleted the person name

I need an employee email-id when I'm selecting his name, if his email id is exist in database. Can any one suggest me how to do, since I have 1400 employees and their mail id, I'm getting their names in dropdown but when I'm selecting their names I need their mail id to display in the particular field......

$("#user_employee_id").change(function(){
         $.ajax({
              type: "GET",
              url: "/User/emailcheck",
              data: { email: user.email }
                });

        });

user_controller.rb
def emailcheck
    @user = User.search(params[:email])
  end

user.rb
def self.search(email)
        if email
            where('email = ?',email).first
        end
  end

Can any one tell how to get the email id when I click on the employee name? I need to get email id of that employee by default in email tab.

Upvotes: 1

Views: 59

Answers (2)

Gaurav Gupta
Gaurav Gupta

Reputation: 1191

You can do this like

Chenge your js Code like:

$("#user_employee_id").change(function(){
  var user_id = $(this).val(); #considering that your are setting id as a value of select field
  $.ajax({
    type: "GET",
    url: "/user/emailcheck",
    data: { id: user_id }
 }).success(function(res){
   $("#your_text_field_id").val(res.text);
 });

});

and in your user_controller.rb

 def emailcheck
   user = User.find_by(id: params[:id])
   if user.present? 
      render text: user.email 
   end  
 end

Upvotes: 1

Pitabas Prathal
Pitabas Prathal

Reputation: 1012

Try this:

$("#user_employee_id").change(function(){
   var id = $(this).val(); 
     $.ajax({
          type: "GET",
          url: "/User/emailcheck",
          data: { id : id}
          success : function( data ) {
           $("#user_email").val(data);
          },
          error   : function(err) {

          }
       });

    });

user_controller.rb

 def emailcheck
   @user = User.find_by_id(params[:id])
   respond_to do |format|
    if @user.email.present?
      format.json { render json: @user.email , status: :ok }
    else
     format.json { render json: "No mail id", status: :unprocessable_entity }
    end
   end
 end

Upvotes: 1

Related Questions