使用带有Rails的jQuery自动完成插件的示例 [英] example for using jquery auto complete plugin with rails

查看:116
本文介绍了使用带有Rails的jQuery自动完成插件的示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果有人可以帮我举例说明在我的Rails应用程序中实现自动完成功能,那将有很大的帮助.我尝试了jquery自动完成插件,但未能实现.

It would be of great help if some one could help me with an example to implment auto complete feature in my rails application.I tried the jquery auto complete plugin.I was not able to achieve.

我的控制器:

def new    
@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)   
respond_to do |format|
  format.html # new.html.erb
  format.xml  { render :xml => @release }
      end
end

我想为@testers创建自动完成

查看代码:

 = form.label :tester_tokens, "Testers" 
 = form.text_field :tester_tokens

感谢您的帮助,

Ramya.

推荐答案

看看宝石 rails3-jquery-autocomplete .它应该是您实施的基础.甚至还有一个示例应用程序,它说明了包含它所必须采取的每个步骤在您的应用程序中.

Have a look at the Gem rails3-jquery-autocomplete. It should be the base for your implementation. There is even an example application that explains each step you have to take to include it in your application.

在Railscasts.com上,您会找到一个情节,说明如何使用它:自动完成关联(修订)

At Railscasts.com, you will find an episode that explains how to use it: Autocomplete-association (revised)

如果它根本不适合您,则应返回并提出具体问题.从上述问题来看,您不清楚要在何处使用自动完成功能.通常,它用于建立与另一个对象的(附加)关联,您要在其中用自动完成字段替换下拉列表或列表中的选择或复选框列表.

If it does not work at all for you, you should come back and ask concrete questions. From your above question, it is not clear where you want to use autocomplete. It is normally used to establish an (additional) association to another object, where you want to replace the drop-down-list or selection-in-list or list of checkboxes by the autocomplete field.

还有一种选择,如果您想选择多个选项,请查看轨道广播情节令牌字段" .因为您的评论指出这是您要执行的操作,所以这里有一些提示(从我的应用程序复制,您必须用您的上下文替换它,它是

There is an alternative, if you want to have more than one thing selected, have a look at the Railscasts episode "Token Fields". Because your comment states that this is what you want to do, here are some hints how to do it (copied from my application, you have to replace it by your context, it is a short version of Railscasts 258):

  • JQuery令牌输入安装到Rails应用程序中.
  • 确保您的应用程序知道jquery(通过使用Gem jquery-rails)
  • 将Javascript文件jquery.tokeninput.js包含到您的应用程序中(语法取决于您使用的版本).
  • 在模型中包含以下代码(User ??):

  • Install JQuery Tokeninput into your Rails application.
  • Ensure that your application knows jquery (by using Gem jquery-rails)
  • Include the Javascript file jquery.tokeninput.js into your application (syntax depends on the version you are using).
  • Include the following code in your model (User ??):

class User < ActiveRecord::Base
  attr_accessible :name, :tester_tokens
  has_many :testers
  attr_reader :tester_tokens

  def tester_tokens=(ids)
    self.tester_ids = ids.split(",")
  end

end

  • 在您的application.js中包含以下代码:

  • Include in your application.js the following code:

    $(function () {
      $('#user_tester_tokens').tokenInput('/testers.json', { 
          crossDomain: false,
          prePopulate: $('#user_tester_tokens').data('pre') })
    }); 
    

  • 在您的TestersController中包含以下代码:

  • Include in your TestersController the following code:

    class TestersController < ApplicationController
      def index
        @testers = Tester.where("name like ?", "%#{params[:q]}%")
        respond_to do |format|
          format.html
          format.json { render :json => @testers.map(&:attributes) }
        end
      end
    end
    

  • 在您的视图代码中更改以下行:

  • Change in your view code the following line:

    = form.text_field :tester_tokens, "data-pre" => @user.testers.map(&:attributes).to_json
    

  • 您将在以下所有步骤中找到有关这些步骤的说明以及更多背景信息,网址为 Railscasts第258集.

    You will find the explanation for all these steps, and some more background at Railscasts episode 258.

    这篇关于使用带有Rails的jQuery自动完成插件的示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆