【发布时间】:2011-07-20 23:26:02
【问题描述】:
我有一个应用,它实现了群组功能。每个组有 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
在 groups
的 index.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。
请在这方面帮助我。对此的任何输入都会非常方便..
感谢您的耐心阅读和时间..
Question Edited
我只想提一下,我是 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 用于自动完成自定义方法的工作如下所示:-
:search}) 做 %> 搜索投资者组: "off"%>
在 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"
使用上述信息,我无法弄清楚如何将属于组的成员总数和组特定的个人资料图片添加到适用于标题的现有自动完成功能。
你能帮我做同样的事情吗?
非常感谢。
【问题讨论】:
-
首先,rails 2.0.2?啊。其次,我建议您放弃 RJS 方法并使用 jQuery 自动完成插件。它可以让您在非常精细的水平上控制显示。
-
谢谢 Srdjan.. 会记住这一点的..
标签: ruby-on-rails autocomplete ruby-1.8 ruby-on-rails-2