简体   繁体   English

在Rails中使用自动完成功能

[英]Using auto-complete in ruby on rails

I'm new to ruby on rails. 我是红宝石的新手。 In fact, I've started working on it from yesterday. 实际上,我从昨天开始就开始研究它。 All the concepts are new to me. 所有概念对我来说都是新的。 I'm stuck with a problem which I need help for. 我遇到了需要帮助的问题。

This is my controller file. 这是我的控制器文件。

    class ProductsController < ApplicationController  
      def new
        #render :layout => 'mylayout'
      end

      def result 
        #@site_matches = Srch.find(:all,:conditions =>["q=?",params[:q]] ) 
        #render :layout => 'mylayout'
      end

      def show
        #render text: params[:products].inspect
        @site_matches = Srch.where("name like ?", "%#{params[:name]}%")
      end
    end

This is my HTML file where I receive my search word and display the result. 这是我的HTML文件,我在其中接收搜索词并显示结果。

    <h1>Results for your search.</h1>
    <%= render "products/new"%>
<table><tr><th>Name</th><th>Max. Price</th><th>Min. Price</th><th>Discount</th><th>Image</th></tr>

    <% for prod in @site_matches %>
    <tr><td><%= prod.name %></td><td><%= prod.maxprice%></td><td><%= prod.minprice%></td><td><%= prod.discount%></td><td> <%= image_tag(prod.image, :alt => "logo", :size => "75x75") %> </td></tr>

    <% end %>
    </table>
    </br>

    <%= link_to "Search again" , "new" %>

I'm able to fetch my data and show the results correctly. 我能够获取数据并正确显示结果。 But I need to add auto-complete feature to this existing one. 但是我需要在此现有功能中添加自动完成功能。 How could I do it i)without using sunspot and ii)also how to do it using sunspot? 我如何在不使用黑子的情况下做到这一点,以及在使用黑子的情况下如何做?

For auto complete you can use Jquery's auto complete utility all you have to do is to provide json data to auto complete. 对于自动完成,您可以使用Jquery的自动完成实用程序,只需提供json数据即可自动完成。 Rails have to_json function which convert your current result to json Rails具有to_json函数,可将当前结果转换为json

或者,您也可以尝试Rails 3 Jquery Autocomplete Gem ,我一直在我的项目中使用它,它是可自定义的,易于安装。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM