在 Rails 中实现多个字段的自动完成
我有一个应用程序,它实现了群组功能。每个小组有n名成员。此外,每个组都有一个组特定的个人资料图片。
我已经能够实现组功能的自动完成,仅记住组名称。我已经参考了以下教程:- http://railsforum.com/viewtopic.php ?id=23188
我正在使用 ruby 1.8.7 和 rails 2.0.2 来实现项目特定目的。我已在适当的目录中安装了 auto_complete 插件。我使用的是 Ubuntu 10.04 操作系统,
我想集成另外两个东西作为组
1> 当前自动完成功能的一部分当我在自动完成文本字段中输入组名称时,我应该还可以看到根据输入的文本显示的适当的组特定个人资料图片。
2>根据自动完成文本字段中输入的文本,我还应该能够看到与每个组对应的成员数量。
我面临着同样的以下障碍:
目前,我实现的基于组名称的基本自动完成工作的代码如下所示:-
groups_controller.rb 中
auto_complete_for :investor_group, :title
在index.html 中的 groups 的 html.erb
<%=stylesheet_link_tag "groups"%>
<%= javascript_include_tag :defaults %>
Search for a investor group: <%= text_field_with_auto_complete :investor_group, :title, {}, {:method => :get} %><%=submit_tag "Search"%>
。 config/routes.rb
中
map.resources :investor_group, :collection => {:auto_complete_for_investor_groups_title => :get }
我当前能够显示特定组的图像并检索通过在组的 index.html.erb 中使用以下代码来确定属于某个组的成员总数:-
<%for inv_group in @investor_groups%>
<div class="inv_group_img" align="center"><%=image_tag "investor_groups/#{inv_group.title}.jpg"%></div>
<div class="inv_group_details">
<%=inv_group.activated_members.size%><br>
<%end%>
视图的对齐方式当前可能很混乱,但这不是我的直接关注点。因此,请忽略同样的事情。
我知道我需要做什么,并且我已经能够在 groups_controller.rb 中编写一些相同的代码
为了获得我需要的内容,我尝试了以下操作:-
def calculate_members_count
@investor_group = InvestorGroup.find(params[:id])
@members_count = @investor_group.activated_members.size
return "@members_count", :title
end
现在这应该给我组标题/名称和members_count。我不确定如何在同一方法中获取图像。
另外,从这里如果它可以正常工作,我有点做了一些猜测工作,以便将更改反映在已经编写的自动完成功能中。我不太确定它们是否正确...但事情就是这样..
我在 index.html.erb 中更改了以下内容
Search for a investor group: <%= text_field_with_auto_complete :investor_group, :calculate_members_count, {}, {:method => :get} %><%=submit_tag "Search"%>
我在 groups_controller.rb< 中更改了以下内容/强> auto_complete_for :investor_group,:calculate_members_count
我真的不确定直到这里我是否正确,如果是这样我真的不知道我现在需要在 routes.rb 中反映什么变化
我也想请问,我是否需要对我的模型进行一些更改?我不这么认为,只是以防万一才问一下。
另外,我想如果自动完成文本字段仅支持对一个属性的查询搜索,在这种情况下,我是否必须定义自定义自动完成搜索以满足我的要求?如果是的话,我真的不知道如何抢占先机,以及如何去做。我现在可能还需要一个自定义的 javascript 来实现这一点。
请帮助我解决这个问题。对此的任何输入都会非常方便。
感谢您的耐心阅读和时间。
问题已编辑
我只是想提一下,我是 Rails 的新手,几乎没有2.5个月的经验。
经过一番搜索,我意识到为了实现多个字段的自动完成功能,我必须使用自定义的自动完成方法。
我已参考以下教程来让自定义自动完成为我工作:- http://cobaltedge.com/auto-complete-text-fields-in-rails-2
我的自定义自动完成方法如下所示:-
def auto_complete_for_investor_group_title
re = Regexp.new("^#{params[:investor_group][:title]}", "i")
#re1 = Regexp.new("^#{params[:investor_group][:title].jpg}", "i")
find_options = { :order => "title ASC", :conditions => ['title LIKE ?', "%#{params[:title]}%"] }
@investor_group = InvestorGroup.find(:all, find_options).collect(&:title).select { |title| title.match re }
render :inline => "<%= content_tag(:ul, @investor_group.map { |title| content_tag(:li, h(title)) }) %>"
end
我的 index.html.erb自动完成自定义方法的工作方式如下所示:-
<% form_tag({:action => :search}) do %>; 搜索投资者组:<%= text_field_with_auto_complete :investor_group, :title, :autocomplete => "off"%><%=submit_tag "搜索"%> <%结束%>
在 routes.rb 中进行的适当更改如下所示: -
map.auto_complete ':controller/:action', :requirements => { :action => /auto_complete_for_\S+/ }, :conditions => { :method => :get }
此代码非常适合在搜索组标题时获取组标题。我想修改此代码以获取组配置文件特定图像以及属于组的成员总数。
上传的图像的类型为 file_field
,并具有名为 :uploaded_data
的属性。
我还需要获取属于某个组的成员总数。目前,图像和属于某个组的成员总数是使用以下代码通过 index.html.erb 分别显示的:-
<%for inv_group in @investor_groups%>
<div class="inv_group contentTableGray">
<div class="inv_group_img" align="center"><%=image_tag "investor_groups/#{inv_group.title}.jpg"%></div>
<div class="inv_group_details">
<span style="float:right;padding-right: 10px;"><%=show_edit_link_for_group(inv_group)%></span>
<%=link_to inv_group.title, :action => :show, :id => inv_group%>
<div style="clear:both;"></div>
<%=truncate(inv_group.description,100)%>
</div>
<div class="inv_group_details" style="width:14%;text-align: center;">
<%=inv_group.activated_members.size%><br>
....( code continues further.. )
我还发现,组的大小可以是使用某些代码在 groups_controller 中获取的方式像这样:-
@investor_group = InvestorGroup.find(params[:id])
@members = @investor_group.activated_members.size
我真的不确定如何修改此代码以用于方法 auto_complete_for_investor_group_title 。
激活的成员被视为来自以下 Investor_group.rb 模型的关联
has_many :activated_members, :through => :investor_group_members, :source => :investor, :conditions => "activated is true"
使用上述信息,我无法弄清楚如何将属于某个组的总成员和组特定个人资料图片添加到已经适用于标题的现有自动完成功能。
你能帮我做同样的事情吗?
多谢。
I have an app, which implements a group feature. Each group has n members. Also, each group has a group specific profile pic to it.
I have been able to implement auto complete for the group feature keeping in mind the group name alone. I have referred to the following tutorial for the same:- http://railsforum.com/viewtopic.php?id=23188
I am using ruby 1.8.7 and rails 2.0.2 for project specific purpose. I have installed the auto_complete plugin in the appropriate directory. I am on Ubuntu 10.04 OS
I would like to integrate two more things as part of the current auto complete feature for groups
1> When I type in a group name in the auto complete text field I should be able to also see an appropriate group specific profile picture which show up based on the entered text.
2> I should also be able to see the number of members that would show up corresponding to each group, based on the entered text in the auto complete text field.
I am facing the following hurdles going about the same:
Currently the code that I have implemented to get basic auto complete works based on group name looks like this:-
in groups_controller.rb
auto_complete_for :investor_group, :title
in index.html.erb of groups
<%=stylesheet_link_tag "groups"%>
<%= javascript_include_tag :defaults %>
Search for a investor group: <%= text_field_with_auto_complete :investor_group, :title, {}, {:method => :get} %><%=submit_tag "Search"%>
in config/routes.rb
map.resources :investor_group, :collection => {:auto_complete_for_investor_groups_title => :get }
I am able to currently display an image for a particular group and retrieve the total number of members belonging to a group by making use of the following code in index.html.erb of groups:-
<%for inv_group in @investor_groups%>
<div class="inv_group_img" align="center"><%=image_tag "investor_groups/#{inv_group.title}.jpg"%></div>
<div class="inv_group_details">
<%=inv_group.activated_members.size%><br>
<%end%>
The alignment of the view might be hay wire currently, but thats not my immediate focus. Thus kindly, ignore the same.
I have an idea of what I need to do and I have been able to write some code for the same in my groups_controller.rb
To get what I require, I tried the following:-
def calculate_members_count
@investor_group = InvestorGroup.find(params[:id])
@members_count = @investor_group.activated_members.size
return "@members_count", :title
end
Now this should give me the group title/name and the members_count. I am not sure how could I fetch the image also within the same method.
Also, from this If it could work correctly, I kinda have done some guess work for the changes to be reflected in the already written autocomplete functionality. I am not too sure if they would be correct... but here it goes..
I changed the following in my index.html.erb
Search for a investor group: <%= text_field_with_auto_complete :investor_group, :calculate_members_count, {}, {:method => :get} %><%=submit_tag "Search"%>
I changed the following in my groups_controller.rb
auto_complete_for :investor_group,:calculate_members_count
I am really not sure if I am correct till here, if so I really can't figure out what change I would now need to reflect in routes.rb
I wanted to also ask, do I need make some changes to my model by any chance. I don't think so, but just asked in case.
Also I guess if the auto complete text field supports query search for only one attribute, in this case would I have to define a customized auto complete search to suit my requirement? If yes I really have no idea on how to get a head start, and how to go about it. I may be just now that there would be also a need of a custom javascript also for this.
Kindly help me on this. Any inputs on this would be really handy..
Thanks for your patient reading and time..
Question Edited
I just wanted to mention, I am a newbie to Rails and have hardly 2.5 months of experience.
After much search, I realized in order to implement the auto complete feature for more than one field, I would have to use a customized auto complete method.
I have referred to the following tutorial to get the customized auto complete working for me:- http://cobaltedge.com/auto-complete-text-fields-in-rails-2
My customized auto complete method looks like this:-
def auto_complete_for_investor_group_title
re = Regexp.new("^#{params[:investor_group][:title]}", "i")
#re1 = Regexp.new("^#{params[:investor_group][:title].jpg}", "i")
find_options = { :order => "title ASC", :conditions => ['title LIKE ?', "%#{params[:title]}%"] }
@investor_group = InvestorGroup.find(:all, find_options).collect(&:title).select { |title| title.match re }
render :inline => "<%= content_tag(:ul, @investor_group.map { |title| content_tag(:li, h(title)) }) %>"
end
My index.html.erb for the auto complete customized method to work looks like this:-
<% form_tag({:action => :search}) do %>
Search for a investor group: <%= text_field_with_auto_complete :investor_group, :title, :autocomplete => "off"%><%=submit_tag "Search"%>
<%end%>
The appropriate changes made in routes.rb look like this:-
map.auto_complete ':controller/:action', :requirements => { :action => /auto_complete_for_\S+/ }, :conditions => { :method => :get }
This code works well for fetching the group title when it is searched for. I want to modify this code to also fetch a group profile specific image and the total number of members belonging to a group.
The image that is uploaded is of type file_field
and has the attribute named :uploaded_data
.
I also need to fetch the total number of members belonging to a group. Currently the image and the total number of members belonging to a group is displayed via index.html.erb separately using the following code:-
<%for inv_group in @investor_groups%>
<div class="inv_group contentTableGray">
<div class="inv_group_img" align="center"><%=image_tag "investor_groups/#{inv_group.title}.jpg"%></div>
<div class="inv_group_details">
<span style="float:right;padding-right: 10px;"><%=show_edit_link_for_group(inv_group)%></span>
<%=link_to inv_group.title, :action => :show, :id => inv_group%>
<div style="clear:both;"></div>
<%=truncate(inv_group.description,100)%>
</div>
<div class="inv_group_details" style="width:14%;text-align: center;">
<%=inv_group.activated_members.size%><br>
....( code continues further.. )
I also have figured out that the size of a group can be some how fetched in the groups_controller using some code like this:-
@investor_group = InvestorGroup.find(params[:id])
@members = @investor_group.activated_members.size
I am really not sure on how to modify this code to for the method auto_complete_for_investor_group_title .
activated members is taken as an association from the following investor_group.rb model
has_many :activated_members, :through => :investor_group_members, :source => :investor, :conditions => "activated is true"
Using the above information, I am unable to figure out how could I add total members belonging to a group and group specific profile pic to the already existing auto complete feature which works for title.
Can you please help me with the same.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,部分而言:
抱歉,有任何英语。
更新:
collect(&:title) 表示“从您找到的所有投资者组中,只给我他们的头衔”。因此,完全删除它,仅使用:
它说“给我你找到的投资者组”,这样你将有一个投资者组数组可以在部分中使用。这样,您就可以在自动完成列表中显示所需的数据,就像在 index.html 中一样,使用“for”语句,将图像、标题和激活的成员大小放入“li”元素内。
抱歉,有任何英语。
重新更新
差不多了。为了使自动完成工作正常,自动完成方法的响应必须是一个列表。因此,部分就像:
每个项目都由 li 标签包裹,并且全部由 ul 标签包裹。如果您查看前面的代码,您会发现它的工作原理正是如此:
一个 ul 内容标记,包裹着包裹标题的 li 内容标记。
我确实将标题和大小分开在两个 erb 中,因为我从未尝试过将两个信息放在同一个中,如果它有效的话,现在就不要这样做。
抱歉,有任何英语。
So, in parts:
Sorry any engrish.
Update:
The collect(&:title) says "from all investor_groups that you find, give me only their titles". So, remove it completely, using only:
It says "give me the investor_groups that you find", so you will have an array of investor_groups to use in the partial. With this, you can show the data that you want in the autocomplete list, like you did in the index.html, with a "for" statement, putting inside the "li" elements the images, the title and the activated members size.
Sorry any engrish.
Re-Update
Almost there. To the autocomplete works, the response from the autocomplete method must be a list. So, the partial would be like:
Each item wrapped by a li tag, and all wrapped by an ul tag. If you look the previous code, this is exactly how it works:
An ul content tag, wrapping li content tags that wrap the titles.
And I did separate the title and the size in two erb, because I never tried put two information in the same, and don't now if it works.
Sorry any engrish.