从控制器动态调用javascript函数

时间:2016-07-07 05:03:31

标签: javascript jquery ruby-on-rails arrays autocomplete

我正在使用jquery自动填充机制。它需要一个运行此javascript的数组,并在输入时显示结果。这就是为什么我需要availableTags数组。但问题是javascript只加载页面加载的时间,它正在捕获初始数组,但我需要在每个关键字输入的类型中动态调用javascript ...所以我该如何解决这个问题呢?

实际上我想从我的控制器功能中调用这个javascript函数。

我的控制器代码是:

class EmployeesController < ApplicationController
skip_before_action :verify_authenticity_token

def new
    @emp=Employee.new
    @emps=Employee.all

 @check=params[:pt]
 puts @check
 x_dynamic=check_ultimate(@check)
 puts "return value"
 puts x_dynamic

 y=string_include(x_dynamic,@emps)
 puts y
 end

 def string_include(substring,data_ref)
 loo=0
 @firstname_array=Array.new

 data_ref.each do |d_ref|  
    puts "in the loop of string_include method"
    my_string=d_ref.compact_string

    if my_string.include? substring
        puts "String "+ my_string+" includes "+substring
        loo=loo+1
        puts loo
        puts d_ref.firstname
        @firstname_array && @firstname_array.push(d_ref.firstname)
    end

 end
 puts "This is the array"
 puts @firstname_array
 return 1
 end

 def create
    @emp=Employee.new(u_params)

  ret_val=string_check(params[:employee][:firstname])
    if ret_val == 1
  ret_one=convert(params[:employee][:firstname],@emp)
  if ret_one == 1
          @emp.save
          redirect_to new_employee_path , :notice => "Inserted!!!" 
  end
    elsif ret_val == 0
        redirect_to new_employee_path , :notice => "Match,Not inserted!!!"

  else
        redirect_to new_employee_path , :notice => ret_val.to_s
  end
  end


  def u_params
  params.require(:employee).permit(:firstname)
  end

  def convert(entered_name,ref)

  count =0
  txt=entered_name.chomp
  size1=txt.to_s.length

  arr1=Array.new
  for j in 0..size1

      arr1 && arr1.push(txt && txt[j])

  end

  for i in 0..size1
      if arr1
      if arr1[i]==" " || arr1[i]=="@" || arr1[i]=="#" || arr1[i]=="{" || arr1[i]=="}" || arr1[i]=="(" || arr1[i]==")" || arr1[i]=="[" || arr1[i]=="]" || arr1[i]=="." || arr1[i]==";" || arr1[i]=="," || arr1[i]=="%" || arr1[i]=="&" || arr1[i]=="*" || arr1[i]=="!" || arr1[i]=="?" || arr1[i]=="$" || arr1[i]=="^" || arr1[i]==":" || arr1[i]=="-" || arr1[i]=="/"
        count=count+1
        arr1 && arr1[i]=""
      end
      end
  end
  temp=arr1 && arr1.join
  final1=temp.gsub(/\s+/, "").downcase

  ref.compact_string=final1
  ref.save
  return 1

  end

  def string_check(first_name)
  count =0;
  temp1=first_name
  supplied_val=temp1

  flag=100
  puts "Entered in string_check method"
  @employees=Employee.all
  t1=Employee.all.count
  puts t1
  @employees.each do |employee|  
    puts "in the loop"
    temp2=employee
    table_val=temp2.compact_string

    size1=supplied_val.to_s.length
    # size2=table_val.to_s.length


    puts table_val
    puts supplied_val

    arr1=Array.new


    for k in 0..size1

      arr1 && arr1.push(supplied_val && supplied_val[k])

    end


    for i in 0..size1
      if arr1
      if arr1[i]==" " || arr1[i]=="@" || arr1[i]=="#" || arr1[i]=="{" || arr1[i]=="}" || arr1[i]=="(" || arr1[i]==")" || arr1[i]=="[" || arr1[i]=="]" || arr1[i]=="." || arr1[i]==";" || arr1[i]=="," || arr1[i]=="%" || arr1[i]=="&" || arr1[i]=="*" || arr1[i]=="!" || arr1[i]=="?" || arr1[i]=="$" || arr1[i]=="^" || arr1[i]==":" || arr1[i]=="-" || arr1[i]=="/"
        count=count+1
        # puts count
        arr1 && arr1[i]=""
      end
      end
    end

    temp_st=arr1 && arr1.join
    final_st=temp_st.gsub(/\s+/, "").downcase



    if final_st==table_val
      puts "Entered in if"
      flag=0
      break
    else
      puts "Entered in else"
      flag=1
    end
  end

  puts @employees.count
  return flag

  end

def check_ultimate(get_val)
puts "Entered in check_ultimate method"
puts get_val
remove_data=get_val.to_s.chomp
#puts remove_data
si1=remove_data.to_s.length

puts si1

arr3=Array.new


    for p in 0..si1

      arr3 && arr3.push(remove_data && remove_data[p])

    end
    puts arr3
    puts "array end,,,,,,,,,,,,,,,,,,,,,,,,,,,,"


    count=0

    for i in 0..si1
      puts i
      if arr3
      #if arr3[i]=='@'
      if arr3[i]==" " || arr3[i]=="@" || arr3[i]=="#" || arr3[i]=="{" || arr3[i]=="}" || arr3[i]=="(" || arr3[i]==")" || arr3[i]=="[" || arr3[i]=="]" || arr3[i]=="." || arr3[i]==";" || arr3[i]=="," || arr3[i]=="%" || arr3[i]=="&" || arr3[i]=="*" || arr3[i]=="!" || arr3[i]=="?" || arr3[i]=="$" || arr3[i]=="^" || arr3[i]==":" || arr3[i]=="-" || arr3[i]=="/"
        count=count+1
        puts count
        arr3 && arr3[i]=""
      end
      end
    end

    temp_st_two=arr3 && arr3.join
    final_st_two=temp_st_two.gsub(/\s+/, "").downcase

    puts final_st_two
    puts "..............the ajax string"
    return final_st_two
 end
end

我的观点文件是:

 <!doctype html>
 <html lang="en">
 <head>
 <meta charset="utf-8">
 <title>jQuery UI Autocomplete - Default functionality</title>
 <link rel="stylesheet"    href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
 <script src="//code.jquery.com/jquery-1.10.2.js"></script>
 <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>


  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>


  $(document).ready(function(){
  $("input").keyup(function(){
    var box = document.getElementById("tags");
    document.getElementById("abc").innerHTML = box.value;
    $.getScript('/employees/new?pt=' + encodeURIComponent(box.value))
  });
  });

  $(function() {
  var availableTags=<%= @firstname_array.inspect.html_safe %>;

  availableTags.toString();
  document.getElementById("aaa").innerHTML = availableTags;
  // document.getElementById("abc").innerHTML = availableTags;

  $( "#tags" ).autocomplete({
  source: availableTags
  });
  });
  </script>
</head>
<body>


<div class="ui-widget">
<hr>
<%= form_for @emp do |f| %>
Firstname: <%= f.text_field :firstname, id: 'tags'  %></br></br>
<%= f.submit %>
<%end%>
<hr>

<h5><%=@msg%></h5>
<h3>Display all employee information</h3></br>
<% if !@emps.blank? %>
<% for item in @emps %>
    <%= item.firstname %> </br>
<%end%>
<%else%>
<%end%>

</div>

<div id="abc"></div>
<div id="aaa"></div>
</body>
</html>

0 个答案:

没有答案