2016-09-06 65 views
-1

嘿傢伙我正在開發一個rails應用程序,它將報價存儲在數據庫中,然後允許您使用簡單的搜索在報價單中進行搜索。我已經實施了搜索表單,但結果沒有出現,我不知道爲什麼。Ruby on rails簡單搜索不顯示結果

控制器:

class BasicsController < ApplicationController 
    def quotations 
    @quotations = Quotation.all 
    if params[:search] 
     @quotations = Quotation.search(params[:search]).order("created_at DESC") 
    else 
     @quotations = Quotation.all.order("created_at DESC") 
    end 

    if params[:quotation] 
     @quotation = Quotation.new(params[:quotation]) 
     if @quotation.save 
     flash[:notice] = 'Quotation was successfully created.' 
     @quotation = Quotation.new 
     end 
    elsif 
     @quotation = Quotation.new 
    end 
    if params[:sort_by] == "date" 
     @quotations = Quotation.order(:created_at) 
    else 
     @quotations = Quotation.order(:category) 
    end 
    end 
end 

型號:

class Quotation < ApplicationRecord 
    def self.search(search) 
    where("author_name LIKE ? OR quote LIKE ?", "%#{search}", "%#{search}") 
    end 
end 

觀點:

<%= form_tag basics_quotations_path, :method => 'get' do %> 
<p> 
<%= text_field_tag :search, params[:search], placeholder: "Search Quotations" %> 
    <%= submit_tag "Search", :name => nil %> 
</p> 
<% end %> 
<h3>Quotations</h3> 
<ul> 
<% for quotation in @quotations %> 
    <li><%= h quotation.author_name %>: <%= h quotation.quote %></li> 
<% end %> 
</ul> 
<br/> 
<% if params[:sort_by] == "date" %> 
<%= link_to "Sort by category", :action => :quotations, :sort_by => :category %> 
<% else %> 
<%= link_to "Sort by date", :action => :quotations, :sort_by => :date %> 
<% end %> 
<hr/> 

<h3>New quotation</h3> 
<%= form_for @quotation, :url => { :action => :quotations } do |form| %> 
<fieldset> 
    <legend>Enter details</legend> 
    <div class="form_row"> 
    <%= form.label :author_name %> 
    <%= form.text_field :author_name, :size => 20, :maxlength => 40 %> 
    </div> 
    <div class="form_row"> 
    <%= form.label :category %> 
    <% @cats = [] %> 
    <% Quotation.select('DISTINCT category').map(&:category).each do |element| %> 
     <% @cats << element %> 
    <% end %> 
    <%= form.select(:category,options_for_select([[@cats[0],1],[@cats[1], 2], [@cats[2],3]])) %> 
    </div> 
    <div class="form_row"> 
    <%= form.label :new_category%> 
    <%= form.text_field :category , :size =>20 , :maxlength => 40 %> 
    </div> 
    <div class="form_row"> 
    <%= form.label :quote %> 
    <%= form.text_area :quote, :rows => 2, :cols => 40, :maxlength => 500 %> 
    </div> 
</fieldset> 
<p> 
<div class="form_row"> 
    <%= form.submit 'Create' %> 
</div> 
</p> 
<% end %> 

路線: Rails.application.routes.draw do get 'basics/quotations' resources :quotation, :quotations # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end

+0

如果您在rails控制檯上執行搜索:任何結果? Rails生成的SQL看起來好嗎? 當你只是返回'''Quotation.all'''而不是搜索? 您的控制器操作混合搜索和創建報價。我會用兩個動作分開。 你可以刪除第一個'@quotations = Quotation.all' –

+0

你說得對mySql查詢是錯誤的語法。我還創建了一個單獨的操作來搜索數據庫。感謝您花時間回覆我的問題 –

回答

0

你已經擁有了一切在一個行動,這是不是很好。您可能想要查看一些可用的簡單rails教程。

論特別search主題,注意你的方法的最後四行...

if params[:sort_by] == "date" 
    @quotations = Quotation.order(:created_at) 
else 
    @quotations = Quotation.order(:category) 
end 

所以不管你SEACH的結果,你會在所有的報價點更換@quotationscreated_atcategory的順序。

+0

是的,在做了一些更多的閱讀之後,我能夠創建一個單獨的操作來搜索數據庫中的報價單。我還必須更改mySql查詢,然後我才能正常工作。感謝您花時間回覆我的問題 –

0

把所有的東西放到一個單獨的動作中並不明智,你的動作應該非常清晰明確。但是你可以實現你想做的事情。

class BasicsController < ApplicationController 
    before_action :new_quotation, only:[:search_quotations,:index,:quotations] 

    def search_quotations 
    respond_to do |format| 

     if params[:search] 
     @quotations = Quotation.search(params[:search]).order("created_at DESC") 
     else 
     @quotations = Quotation.all.order("created_at DESC") 
     end 

     if params[:sort_by] == "date" && quotations.present? 
     @quotations = @quotations.order(:created_at) 
     else 
     @quotations = @quotations.order(:category) 
     end   
     format.js{} 
    end 
    end 


def quotations 
    if params[:quotation] 
    @quotation = Quotation.new(quotation_params) 
    if @quotation.save 
     flash[:notice] = 'Quotation was successfully created.'    
    end  
    redirect_to root_path 
    end  
end 

def index 
    @quotations = Quotation.all  
end 


private:  

    def new_quotation 
    @quotation = Quotation.new 
    end 

//If you are using rails4 for later version then go for this line. 
def quotation_params 
    params.require(:quotation).permit(:author_name, :quote,:category) 
end 

end 

你需要真正邏輯分成上述行動,並在那裏「報價」行動是爲創建一個新的報價,「seach_quotation」是搜索所有的報價,應該回報JS的原因,我們的反應會在呈現部分'_list.html.erb'時需要此。

您的看法(index.htm.erb)將看起來像這樣。

<div> 
    <%= form_tag basics_search_quotations_path, :method => 'get', remote: true do %> 
    <p> 
     <%= text_field_tag :search, params[:search], placeholder: "Search Quotations" %> 
     <%= submit_tag "Search", :name => nil %> 
    </p> 
    <% end %> 
</div> 
    #This partial will be used for refreshing the quotations list via remote true feature for searching and sorting. 
    <div id="quotation_list"> 
    <%= render 'basics/shared/list',{quotations: @quotations} %> 
    </div> 
br/> 
<% if params[:sort_by] == "date" %> 
    <%= link_to "Sort by category", :action => :search_quotations, :sort_by => :category, :remote => true %> 
<% else %> 
    <%= link_to "Sort by date", :action => :search_quotations, :sort_by => :date, :remote => true %> 
    <% end %> 
<hr/> 

<h3>New quotation</h3> 
    <%= form_for @quotation, :url => { :action => "quotations", :controller => "basics" } do |form| %> 
    <fieldset> 
     <legend>Enter details</legend> 
     <div class="form_row"> 
     <%= form.label :author_name %> 
     <%= form.text_field :author_name, :size => 20, :maxlength => 40 %> 
     </div> 
     <div class="form_row"> 
     <%= form.label :category %>  
     <%=  form.select(:category,options_for_select(['Love','Romance','Sadness'])) %> 
     </div> 
     <div class="form_row"> 
     <%= form.label :category%> 
     <%= form.text_field :category , :size =>20 , :maxlength => 40 %> 
     </div> 
     <div class="form_row"> 
     <%= form.label :quote %> 
     <%= form.text_area :quote, :rows => 2, :cols => 40, :maxlength => 500 %> 
     </div> 
    </fieldset> 
    <p> 
    <div class="form_row"> 
    <%= form.submit 'Create' %> 
    </div> 
</p> 
<% end %> 

這裏是顯示屏報價列表/basics/shared/_list.html.erb

<h3>Quotations</h3> 
    <ul> 
    <% for quotation in @quotations %> 
     <li><%= h quotation.author_name %>: <%= h quotation.quote %></li> 
    <% end %> 
    </ul> 

以下是你需要的路由添加的routes.rb部分

resources :quotation, :quotations 
get "basics/search_quotations" => "basics#search_quotations" 
post "basics/quotations" => "basics#quotations" 
root 'basics#index' 

而不是在視圖中執行任何計算,如果需要,最好在控制器/模型中執行它。

所以不是在你看來這下面一行

<% @cats = [] %> 
    <% Quotation.select('DISTINCT category').map(&:category).each do |element| %> 
     <% @cats << element %> 
    <% end %> 
    <%= form.select(:category,options_for_select([[@cats[0],1],[@cats[1], 2], [@cats[2],3]])) %> 

您可以創建一個實例變量假設@categories或東西,像這樣使用

<%= form.select(:category,options_for_select(@categories)) %> 

而且最後但並非最不重要我們必須有一個search_quotations.js.erb,因爲我們發送一個Ajax請求來獲取搜索結果並返回'js'響應。

$("#quotation_list").html("<%= escape_javascript(render('basics/shared/list', {quotations: @quotations })) %>")