实现自动完成对Rails的多个字段 [英] Implementing auto complete for more than one field in Rails

查看:178
本文介绍了实现自动完成对Rails的多个字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它实现一组功能。每个组有n个成员。另外,每个组都有一组特定的个人资料图片吧。

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.

我已经能够实现自动完成的功能组单独的组名牢记。我已经提到了以下教程相同: - http://railsforum.com/viewtopic.php ?ID = 23188

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

我使用的红宝石1.8.7 栏杆2.0.2 作为项目的具体目的。我已经安装在相应的目录中auto_complete插件。我在的Ubuntu 10.04 操作系统

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>当我在自动完成文本字段组名称中键入我应该可以见相应的组特定的个人资料图片明显显示出基于输入的文字。

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>我也应该能看到成员,将显示对应于每个组的数量的基础上,在自动完成文本字段中输入的文本。

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:

目前,我已经实现了获得基于组的名称基本自动完成的作品code如下: -

Currently the code that I have implemented to get basic auto complete works based on group name looks like this:-

groups_controller.rb

auto_complete_for :investor_group, :title

集团 index.html.erb

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"%>

的config / routes.rb中

in config/routes.rb

map.resources :investor_group, :collection => {:auto_complete_for_investor_groups_title => :get }

我能够通过使用以下code的在 index.html.erb 作为一个特殊群体,以目前显示图像,并检索属于组成员的总数组: -

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.

我有什么,我需要做的一个想法,我已经能够写一些code为同一我groups_controller.rb

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

现在这应该给我组标题/名称和MEMBERS_COUNT。我不知道我怎么能取也同样的方法中的形象。

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

我改变了下面在我的 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

I changed the following in my groups_controller.rb auto_complete_for :investor_group,:calculate_members_count

我真的不知道,如果我是正确的到这里,如果让我实在想不出有什么变化我现在需要反映的的routes.rb

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.

此外,我想,如果自动完成文本字段支持查询搜索只有一个属性,在这种情况下,我将不得不定义,以满足我的要求定制的自动完成搜索?如果是的话我真的如何获得一个良好的开端,以及如何去做不知道。我可能是刚才会有还需要自定义也为这个JavaScript的。

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

我只想说,我是一个新手,Rails和几乎没有2.5个月的经验。

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.

我已经提到了下面的教程,以获得定制的自动完成的工作对我来说: - 的 http://cobaltedge.com/auto-complete-text-fields-in-rails-2

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

我的 index.html.erb 作为自动完成定制的方法来工作如下: -

My index.html.erb for the auto complete customized method to work looks like this:-

&LT;%的form_tag({:动作=>:搜索})办%>
       搜索一个投资者的基团:;%= text_field_with_auto_complete:investor_group,:标题:自动完成=>关闭%>&LT;%= submit_tag搜索%>
    &LT;%结束%>

<% form_tag({:action => :search}) do %> Search for a investor group: <%= text_field_with_auto_complete :investor_group, :title, :autocomplete => "off"%><%=submit_tag "Search"%> <%end%>

在做出适当的修改的routes.rb 是这样的: -

The appropriate changes made in routes.rb look like this:-

  map.auto_complete ':controller/:action', :requirements => { :action => /auto_complete_for_\S+/ }, :conditions => { :method => :get }

这code很适合当它被搜索获取该组冠军。我想修改此code也取一组配置文件的具体形象,属于组成员的总数。

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.

这是上传的图片类型的是,file_field 并拥有命名的属性:uploaded_data

The image that is uploaded is of type file_field and has the attribute named :uploaded_data .

我还需要获取属于一组成员的总数量。目前图像并属于一组成员的总数量是通过index.html.erb显示分别使用以下code: -

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

我也想通了,一个组的大小如何获取在groups_controller使用一些code这样一些: -

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

我真的不知道如何修改此code为方法auto_complete_for_investor_group_title。

I am really not sure on how to modify this code to for the method auto_complete_for_investor_group_title .

激活构件被取为从以下investor_group.rb模型的关联

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"

利用上面的信息,我无法弄清楚如何我可以添加属于一个组,组特定的配置文件的成员总数PIC 以已经存在的自动完成功能,适用于标题。

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.

非常感谢。

推荐答案

所以,在部分:


  1. 当你得到投资者群体,你只得到与呼叫收(放;:标题)的称号。删除它得到整个对象;

  2. 您不需要使用正则表达式,你已经获得与像条件匹配的标题; <​​/ li>
  3. 而不是渲染内联,尽量使局部的呈现所有你所需要的LI标签内。渲染内联和content_tag都不错,当所有你需要的是一个小的文本,但更大的事情,preFER谐音。

对不起任何engrish。

Sorry any engrish.

更新:

的collect(安培;:标题)说:从找到的所有investor_groups,给我只有他们的头衔。因此,彻底清除它,只有使用:

The collect(&:title) says "from all investor_groups that you find, give me only their titles". So, remove it completely, using only:

InvestorGroup.find(:all, find_options)

它说:给我你找到investor_groups,所以你将有investor_groups阵列在部分使用。有了这个,你可以证明你在自动完成列表中选择所需的数据,就像你在做的index.html,以为语句,把礼分子图像,标题和激活的成员里面的大小。

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.

对不起任何engrish。

Sorry any engrish.

重新更新

几乎没有。要自动完成的作品,从自动完成的方法的反应必须是一个列表。因此,部分会像:

Almost there. To the autocomplete works, the response from the autocomplete method must be a list. So, the partial would be like:

<ul>
  <% for inv_group in @investor_group2 %>
    <li><%=h inv_group.title %>, <%=h inv_group.activated_members.size %></li>
  <%end%>
</ul>

每个项目由李标签包装,以及所有通过UL标签包裹。如果你看看previous code,这正是它是如何工作的:

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:

content_tag(:ul, @investor_group2.map { |title| content_tag(:li, h(title)) })

这是UL标签的内容,即包的标题包装里的内容标签。

An ul content tag, wrapping li content tags that wrap the titles.

和我没有分开的称号,并在两个ERB的大小,因为我从来没有尝试过把两个信息一样,现在不,如果它的工作原理。

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.

对不起任何engrish。

Sorry any engrish.

这篇关于实现自动完成对Rails的多个字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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