使用 jquery 自动完成插件与 Rails 的示例

发布于 2024-12-23 07:09:23 字数 678 浏览 0 评论 0原文

如果有人可以帮助我举一个例子来在我的 Rails 应用程序中实现自动完成功能,那将会有很大的帮助。我尝试了 jquery 自动完成插件。但我无法实现。

我的控制器:

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。

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.

My controller:

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

I want to create an auto complete for the @testers

view code:

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

Thanks for your help,

Ramya.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

肤浅与狂妄 2024-12-30 07:09:23

查看 Gem rails3-jquery-autocomplete。它应该是您实施的基础。甚至还有一个 示例应用程序 解释了包含它所需执行的每个步骤在您的应用程序中。

在 Railscasts.com,您会找到一集解释如何使用它:自动完成-协会(已修订)

如果它对您根本不起作用,您应该回来提出具体问题。从您上面的问题来看,尚不清楚您想在哪里使用自动完成功能。它通常用于建立与另一个对象的(附加)关联,您希望用自动完成字段替换下拉列表或列表中的选择或复选框列表。

还有一种选择,如果您想选择多个内容,请查看 Railscasts 剧集“令牌字段”。因为您的评论表明这就是您想要做的,所以这里有一些如何做到这一点的提示(从我的应用程序复制,您必须用您的上下文替换它,它是 Railscasts 258):

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

    类用户< ActiveRecord::基础
      attr_accessible :名称,:tester_tokens
      has_many:测试人员
      attr_reader :tester_tokens
    
      def tester_tokens=(ids)
        self.tester_ids = ids.split(",")
      结尾
    
    结尾
    
  • 在您的 application.js 中包含以下代码:

    <前><代码>$(函数 () {
    $('#user_tester_tokens').tokenInput('/testers.json', {
    跨域:假,
    预填充:$('#user_tester_tokens').data('pre') })
    });

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

    类 TestersController <应用控制器
      定义索引
        @testers = Tester.where("名称类似于?", "%#{params[:q]}%")
        respond_to do |格式|
          格式.html
          format.json { 渲染 :json =>; @testers.map(&:属性) }
        结尾
      结尾
    结尾
    
  • 在视图代码中更改以下行:

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

您将在 Railscasts 第 258 集

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.

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):

  • 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
    
  • Include in your application.js the following code:

    $(function () {
      $('#user_tester_tokens').tokenInput('/testers.json', { 
          crossDomain: false,
          prePopulate: $('#user_tester_tokens').data('pre') })
    }); 
    
  • 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
    

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文