【问题标题】:Displaying a pop up message in javascript for my rails application在 javascript 中为我的 rails 应用程序显示弹出消息
【发布时间】:2015-04-01 11:42:35
【问题描述】:

问题在我的 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

这是我的看法..

<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.');
       }
     });}
   );
 });

【问题讨论】:

  • 究竟是什么不起作用?

标签: javascript ruby-on-rails ruby ajax ruby-on-rails-4


【解决方案1】:

.您可以使用不同的ajax callbacks来检查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

查看文件,带占位符

<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();
                  }
       });
     }
   );
 });

【讨论】:

  • @Legendary...不是错字...如果出现错误,他需要捕捉响应...所以我编辑了我的答案。看看..
  • 现在好了,只是我不明白,为什么你要更改代码,当你看到语法错误时? @Milind
  • @Legendary..你不能在json响应中使用flash..它需要发送一个json响应而不是flash..我们需要在js代码中使用它...
  • @Milind 您的回答适用于它是否找到有效的 id,但如果 id 不存在则仍然没有运气。我的 js 正在查看 emp_first_name 文本框,但是,当没有为 emp_first_name 返回数据时,它似乎不起作用,但是当 emp_id 匹配并填写 emp_first_name 时,我得到您的员工 id 已找到-----.. ..我的问题是如何让它告诉我没有找到 id
  • @Milind - “我的评级生活”
【解决方案2】:

控制器:

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

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.length === undefined )) $('#emp_last_name').val(data.emp_last_name);
       if( (data.error * 1) == 404) {
         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.");
       }
     });
   }
   );
 });

【讨论】:

  • 当我在 emp_first_name 之后添加括号时,它给了我这个错误 SyntaxError: expected expression, got '===' @Legendary
  • 你能显示你的 console.log(data);在 ajax.success 之后
  • $.getJSON(url, function(data) { console.log(data); 并从 js 控制台显示给我们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-10
  • 1970-01-01
相关资源
最近更新 更多