简体   繁体   English

ruby on rails实现了通用搜索

[英]ruby on rails implement generic search

I have 2 tables in my DB - an "illness" table and a "symptoms" table. 我的数据库中有2个表 - 一个“疾病”表和一个“症状”表。 I've implemented a generic search for searching both tables. 我已经实现了搜索两个表的通用搜索。 My goal is to display the results in the result page, each result should be a hyperlink that leads to the result "show" page (illness/id/show or symptom/id/show). 我的目标是在结果页面中显示结果,每个结果应该是一个超链接,导致结果“显示”页面(疾病/ id / show或症状/ id / show)。

As i'm passing generic results to the result page, I don't really know whether the current result is an illness or a symptom. 由于我将通用结果传递给结果页面,我真的不知道当前结果是疾病还是症状。 I wonder what is the best way to get this information (Should I try to collect this informaiton in the controller and somehow pass it to the html? should I somehow run another query from the html?) 我想知道获取此信息的最佳方式是什么(我应该尝试在控制器中收集此信息并以某种方式将其传递给html?我应该以某种方式从html运行另一个查询吗?)

I'm using rails 3.x, and my controller code looks like this: 我正在使用rails 3.x,我的控制器代码如下所示:

class SearchController < ApplicationController
    def index
        @results = Illness.search(params[:search]) + Symptom.search(params[:search])

        respond_to do |format|
            format.html # index.html.erb
            format.json { render json: @results }
        end
    end
end

Thanks, Li 谢谢,李

You don't have to be worried about it. 你不必担心它。 Let the Rails to serve it: 让Rails服务它:

   - @results.each do |result|
     = link_to 'Show', result

And you'll get the proper link based on the result's type. 并且您将根据结果的类型获得正确的链接。

And one more. 还有一个。 What the show in URL has to do with the show action in your examples like: symptom/id/show ? 什么show的网址已与做show在你的例子一样的动作symptom/id/show The show action is mapped by default to GET /model/id path. show动作默认映射到GET /model/id路径。

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

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