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

查看:21
本文介绍了使用带有 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

感谢您的帮助,

拉米亚.

推荐答案

看看 Gem 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.

还有另一种选择,如果您想选择多个事物,请查看 Railscasts 剧集Token Fields".因为您的评论表明这是您想要做的,所以这里有一些提示(从我的应用程序复制,您必须用您的上下文替换它,它是 Railscasts 258):

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 Tokeninput 安装到您的 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 中包含以下代码:

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

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

    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 集.

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

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