2016-09-06 3 views
-1

あなたはデータベースに引用符を格納し、簡単な検索を使って引用符を検索することができるレールアプリケーションを開発しています。私は検索フォームを実装しましたが、結果は表示されず、理由がわかりません。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で生成されたSQLは正常に見えますか? 検索の代わりに '' 'Quotation.all'''を返すだけですか? コントローラのアクションによって、検索と見積の作成が混在します。私はそれを2つのアクションで分けています。 最初の '@quotations = Quotation.all'を削除することができます –

+0

あなたは誤った構文であるmySqlクエリについて正しくありました。私はまた、データベースを検索するために別個のアクションを作成しました。 –

答えて

0

あなたが持っているEVERYTHING 1つのアクションで、それは素晴らしいことではありません。利用可能なシンプルなレールチュートリアルのいくつかを見直すことをお勧めします。特にsearchをテーマに

、あなたの方法の最後の4行を注意し

...だから関係なく、あなたのさがすの結果の

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

、あなたはすべて引用して、その時点で@quotationsを置き換えますcreated_atまたはcategoryオーダーのいずれかです。

+0

もう少し読んだ後、引用をデータベースで検索するために別のアクションを作成することができました。私はまた、MySQLのクエリを変更する必要があったし、私はそれを働かせた。私の質問に答える時間をとってくれてありがとう –

0

すべてのものを1つのアクションに配置するのは賢明ではありませんが、アクションは非常に明確でよく定義されている必要があります。

あなたが実際には「引用」アクションが新たな引用と「seach_quotation」を作成するためのものです上記のアクションとにロジックを分離するために必要
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 

はすべて引用を検索するためのものであり、それは我々の原因を返す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 %> 

はここ

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

はあなたが

resources :quotation, :quotations 
get "basics/search_quotations" => "basics#search_quotations" 
post "basics/quotations" => "basics#quotations" 
root 'basics#index' 
をroutes.rbを追加する必要があるルート/basics/shared/_list.html.erb引用のリストを表示している部分であります

ビューで計算を実行するのではなく、必要に応じてコントローラ/モデルで実行する方がよいでしょう。

ので、代わりにあなたがのが@categoriesか何かを言わせて、これ

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

そして最後のようにそれを使用するインスタンス変数を作成することができます

<% @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]])) %> 

あなたのビューで、この次の行のではなく、少なくともsearch_quotations.js.erbを持つ必要があります。なぜなら、検索結果を取得し、 'js'レスポンスを返すためのajaxリクエストを送信しているからです。

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