Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中为我的rails应用程序显示弹出消息_Javascript_Ruby On Rails_Ruby_Ajax_Ruby On Rails 4 - Fatal编程技术网

在javascript中为我的rails应用程序显示弹出消息

在javascript中为我的rails应用程序显示弹出消息,javascript,ruby-on-rails,ruby,ajax,ruby-on-rails-4,Javascript,Ruby On Rails,Ruby,Ajax,Ruby On Rails 4,问题是在我的rails应用程序中,我的用户控制器中有一个自动填充的方法。这将在新用户注册时为他们填写表单。当他们填写“我的emp_id”文本框时,它将查看我的员工表,我从中提取数据。如果id与他们键入的emp_id匹配,它将自动填充他们的emp_first_name和emp_last_name。我能够有一个弹出消息时,数据返回有数据,但我坚持如何使它显示一个消息,如果没有数据存在的特定emp__id 这是我的控制器 class UserController < ApplicationCon

问题是在我的rails应用程序中,我的用户控制器中有一个自动填充的方法。这将在新用户注册时为他们填写表单。当他们填写“我的emp_id”文本框时,它将查看我的员工表,我从中提取数据。如果id与他们键入的emp_id匹配,它将自动填充他们的emp_first_name和emp_last_name。我能够有一个弹出消息时,数据返回有数据,但我坚持如何使它显示一个消息,如果没有数据存在的特定emp__id

这是我的控制器

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
控制器:

class UserController < ApplicationController

  def populate_form

    @visual = Visual.find_by_id(params[:emp_id])


    unless @visual.present?

      render json: {user: @user, status: :unprocessable_entity, error: 404}

    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
。您可以使用“不同”来检查json响应是否失败,然后显示错误消息。您可以使用空白占位符来显示警报消息。例如:-

在控制器中使用此选项显示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
class UserController{
:emp_first_name=>@emp_first_name,
:emp_last_name=>@emp_last_name
}
其他的
format.js{render:json=>id不存在,:status=>400}
format.html{render:json=>id不存在,:status=>400}
结束
结束
结束
查看文件,带有占位符

<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();
                  }
       });
     }
   );
 });
$('#emp_id')。更改(函数(){
var url=“/user/populate_form?emp_id=“+$(this).val()”;
$.getJSON(url、函数(数据){
如果(!(data.emp_first_name==未定义))
$('#emp_first_name').val(data.emp_first_name);
如果(!(data.emp_last_name==未定义))
$('emp_last_name').val(data.emp_last_name);
}).完成(功能(响应){
如果(response.success=='success'){
//警惕(“成功”);
$(“#显示提醒”).html(“已找到您的员工ID,请填写电子邮件和密码字段,然后单击提交以注册…”)
}否则{
//警报(“失败”);
$(“#显示提醒”).html(“未找到失败的员工ID,请使用有效的员工ID重试”).show();
}
});
}
);
});

什么不起作用?当我在emp_first_name后面添加括号时,它给了我这个错误语法错误:预期表达式,得到'=='@legendary你可以显示你的控制台.log(数据);紧接着ajax.success$.getJSON(url,函数(数据){console.log(数据);并从js向我们展示console@Legendary...its不是输入错误。如果出现错误,他需要捕获响应…所以我已经编辑了我的答案。看一看。好吧,现在我不明白,当你看到语法错误时,为什么你要更改代码@Milind@Legendary..您不能在json响应中使用flash。它需要发送json响应,而不是flash。这我们需要在js代码中使用它…@Milind your answer适用于它是否找到有效id,但如果id不存在,它仍然不走运。我的js正在查看emp_first_name文本框,但是,当没有返回emp_first_name的数据时,它似乎不起作用,但是当emp_id匹配并填写emp_first_name时,我会得到您的员工id-----……我的问题是,我怎样才能让它告诉我没有身份证found@Milind-“我的评级生活”
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>
 $('#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();
                  }
       });
     }
   );
 });