使用 jquery 自动完成插件与 Rails 的示例
如果有人可以帮助我举一个例子来在我的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
查看 Gem rails3-jquery-autocomplete。它应该是您实施的基础。甚至还有一个 示例应用程序 解释了包含它所需执行的每个步骤在您的应用程序中。
在 Railscasts.com,您会找到一集解释如何使用它:自动完成-协会(已修订)
如果它对您根本不起作用,您应该回来提出具体问题。从您上面的问题来看,尚不清楚您想在哪里使用自动完成功能。它通常用于建立与另一个对象的(附加)关联,您希望用自动完成字段替换下拉列表或列表中的选择或复选框列表。
还有一种选择,如果您想选择多个内容,请查看 Railscasts 剧集“令牌字段”。因为您的评论表明这就是您想要做的,所以这里有一些如何做到这一点的提示(从我的应用程序复制,您必须用您的上下文替换它,它是 Railscasts 258):
jquery-rails
)jquery.tokeninput.js
包含到您的应用程序中(语法取决于您使用的版本) 。在您的模型中包含以下代码(
User
??):在您的
application.js
中包含以下代码:<前><代码>$(函数 () {
$('#user_tester_tokens').tokenInput('/testers.json', {
跨域:假,
预填充:$('#user_tester_tokens').data('pre') })
});
在您的
TestersController
中包含以下代码:在视图代码中更改以下行:
您将在 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):
jquery-rails
)jquery.tokeninput.js
into your application (syntax depends on the version you are using).Include the following code in your model (
User
??):Include in your
application.js
the following code:Include in your
TestersController
the following code:Change in your view code the following line:
You will find the explanation for all these steps, and some more background at Railscasts episode 258.