2014-10-06 47 views
3

我試圖在單獨的控制器中對模型執行復雜搜索。我有一個學生模型。整個應用程序有一個單獨的main_controller處理的頭版,它沒有模型。 main_controller及其相關的索引視圖應該提供來自多個模型的首頁和顯示數據。Rails 4在單獨的控制器中搜索幾個模型

現在我想用幾種不同類型的搜索條件搜索模型。搜索標準是字符串比較,數字比較和布爾值(例如,如果爲真,則只顯示活動學生顯示所有學生)。 Railscast #111展示瞭如何基於模型和單獨的搜索控制器創建這樣的搜索。我創建了這樣一個控制器,它工作正常。我堅持在主/索引中顯示相關部分。

下面是代碼:

主/ index.html.haml

- model_class = Adult 
- model_class = Pupil 
- model_class = MainSearch 

.page-header 
= render :partial => 'main_searches/form', :main_search => MainSearch.new 

只需調用這裏此刻的形式。

型號/ main_search.rb

class MainSearch < ActiveRecord::Base 

    def pupils 
    @pupils ||= find_pupils 
    end 

    private 
    def find_pupils 
     pupils = Pupil.order(:name_sur) 
     pupils = pupils.where(id: id_search) if id_search.present? 
     pupils = pupils.where("name_first like ?", "%#{name_first}%") if name_first.present? 
     pupils = pupils.where("name_sur like ?", "%#{name_sur}%") if name_sur.present? 
     pupils = pupils.where(active: "true") if active == true #show only active or all 
     pupils 
    end 
end 

這個定義搜索。

控制器/ main_searches_controller.rb

class MainSearchesController < ApplicationController 
    before_action :set_main_search, only: [:show, :update, :destroy] 

    def show 
    @main_search = MainSearch.find(params[:id]) 
    end 

    def new 
    @main_search = MainSearch.new 
    end 

    def create 
    @main_search = MainSearch.new(main_search_params) 

    if @main_search.save 
     redirect_to root_path, notice: 'Main search was successfully created.' 
    else 
     render action: 'new' 
    end 
    end 
end 

如railscast所示。

視圖/ main_searches/_form.html.haml

%h1 Advanced Search 

    = form_for :main_search, :url => main_searches_path do |f| 
    .field 
     = f.label :id_search 
     %br/ 
     = f.text_field :id_search 
    [... ommitted some fields here ...] 
    .field 
     = f.label :active 
     %br/ 
     = f.check_box :active, {}, true, false 


    .actions= f.submit "Search" 

在新的視圖中呈現。

的意見/ main_searches/_results.html.haml

%h1 Search Results 

.container-fluid 
    .row-fluid 
    .span4 

     %table.table{style: "table-layout:fixed"} 
     %thead 
      %tr 
      %th= "id" 
      %th= "name_sur" 
      %th= "name first" 
      %th= "a" 
     %div{style: "overflow-y:scroll; height: 200px"} 
     %table.table.table-striped{style: "table-layout:fixed"} 
      %tbody 
      - @main_search.pupils.each do |pupil| 
       %tr 
       %td= pupil.id 
       %td= link_to pupil.name_sur, pupil_path(pupil) 
       %td= pupil.name_first 
       %td= pupil.active 

顯示結果。

因此,基本上一切都適用於一個模型,如在railscast中所見。我現在需要的 是讓用戶以某種方式處理main_controller中的所有內容。此刻我不能 獲得傳遞給_results.html.haml部分的@main_search對象。我在這裏錯過了什麼? 或者這甚至是做這種搜索的正確方法?

感謝您的幫助提前。

+0

[這是一種方法](https://github.com/codeschool/FeatureFocus/blob/basecamp/app/models/search.rb)我在[視頻](https://www.codeschool。 com/screencasts/basecamp-search)。在你的控制器中,你可以這樣調用它:'@main_search = Search.for(params [:id])''。 – Brian 2014-10-06 19:04:30

+0

是否存在想要在數據庫中存儲搜索的特定原因?我瞭解功能,但似乎特別是對多模型搜索,這可能會變得笨拙,因爲每個模型可能會有它自己的一套特點。這些搜索是否與用戶或其他實體相關聯?如果看起來不像創建抽象(缺少更好的單詞)模型更有意義,而是通過它來運行搜索。 – engineersmnky 2014-10-06 19:25:53

+0

@Brian 謝謝,我會在早上檢查第一件事情。 --engineersmnky 你是對的,它可能會變得混亂。起初,我想存儲基於用戶的搜索,但在與可選的客戶覈對之後。你對抽象模型究竟意味着什麼?時態表? – mandulis 2014-10-06 21:32:16

回答

2

Brians解決方案實際上工作得很好。我創建了一個沒有相關數據庫的獨立模型,並將所有搜索邏輯放入該文件中。注意,它只是一個類,與ActiveRecord無關。

class MainSearch 

    def self.for(id_search, name_sur, name_first, active) 

     #escape for security 
     name_sur = "%#{name_sur}%" 
     name_first = "%#{name_first}%" 
     active = "%#{active}%" 

     pupils = Pupil.order(:name_sur) 
     pupils = pupils.where(id: id_search) if id_search.present? 
     pupils = pupils.where("name_first like ?", "%#{name_first}%") if name_first.present? 
     pupils = pupils.where("name_sur like ?", "%#{name_sur}%") if name_sur.present? 
     pupils = pupils.where(active: "true") if active == true #show only active or all 
    end 
end 

我main_controller現在可以調用這個函數,並從params哈希表傳遞值:

@pupils = MainSearch.for(params[:id_search], params[:name_sur], params[:name_first], params[:active]) 

的參數來自視圖/主/ _form_search.html。haml:

.search-box 
    = form_tag root_path, method: :get do 

    = label_tag :id_search 
    %br/ 
    = text_field_tag 'id_search', nil 
    %br/ 
    = label_tag :name_sur 
    %br/ 
    = text_field_tag 'name_sur', nil 
    %br/ 
    = label_tag :name_sur 
    [...] 
    = submit_tag "Submit" 

最後一步是顯示結果。正如你在main_controller中看到的那樣,我只需填充@pupils對象,從而可以在任何視圖中輕鬆地呈現它。下面是從視圖/主/ index.html.haml

.span8 
    .row-fluid 
    %table.table{style: "table-layout:fixed"} 
     %thead 
     %tr 
      %th= "id" 
      %th= sortable "name_sur" 
      %th= "name first" 
      %th= "a" 
    %div{style: "overflow-y:scroll; height: 200px"} 
     %table.table.table-striped{style: "table-layout:fixed"} 
     %tbody 
      - @pupils.each do |pupil| 
      %tr 
       %td= pupil.id 
       %td= link_to pupil.name_sur, pupil_path(pupil) 
       %td= pupil.name_first 
       %td= pupil.active 

    = render :partial => 'form_search' 

此安裝程序在main_controller所有的控制,只需要一個外部文件(搜索模式)的摘錄。在這個模型中,我可以在多個模型中搜索數據(如視頻中所示),並可以根據我的需要合併搜索條件,甚至可以在其中執行一些性能惡意軟件。我對結果很滿意。所以感謝所有的投入。

+0

很高興爲你效勞。 – Brian 2014-10-08 19:17:07

相關問題