使用带有rails应用程序的jquery自动完成插件

时间:2011-12-23 10:37:10

标签: jquery ruby-on-rails-3 jquery-plugins autocomplete

在我的rails应用程序中,我正在尝试使用自动完成插件。它不能正常工作。请在这里帮助我。

查看文件:

%td.grid.full_panels
    -table_panel "Assign Testers" do
      %table
        %th Name
        -puts "testers=#{@testers}"
        = form.label :tester_tokens, "Testers" 
        = form.text_field :tester_tokens,:class=>"users_auto_complete" 
        - @testers.each do |tester|
          %tr
            %td=tester.name
            %td=check_box_tag "release[tester_ids][]", tester.id,   @release.is_tester_assigned(tester)
      =will_paginate @testers
@testers=['sramya','scharron','jstinson']
:javascript  
data=#{@tmp_testers.to_json}
= javascript_include_tag "pages/ic", :cache => true

控制器文件:

def new
@release = Release.new
@ic_ids = params[:ic_ids] ? params[:ic_ids] : []
 @testers = User.find_by_sql("select * from users where id in(select user_id from user_role_assignments where role_id in (select id from roles where name like 'Tester')) order by name").paginate(:page=>params[:page],:per_page=>30)   
 if params[:project_id]
  @release.project = Project.find(params[:project_id])
end
respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @release }
end
end

使用Javascript:

$('.users_auto_complete').live("keypress",function() {
var data= #{@tmp_testers.to_json}
alert (data);
$("#release_tester_tokens").autocomplete(data);
});

1 个答案:

答案 0 :(得分:0)

你的jquery是(document).ready函数

$(document).ready(function(){

$('.users_auto_complete').live("keypress",function() {
var data= #{@tmp_testers.to_json}
alert (data);
$("#release_tester_tokens").autocomplete(data);
});

});