Ruby on rails简单搜索不显示结果

时间:2016-09-06 10:14:46

标签: ruby-on-rails ruby ruby-on-rails-3

嘿伙计们正在开发一个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

2 个答案:

答案 0 :(得分:0)

你在一次行动中得到了一切,这并不是很好。您可能希望查看一些可用的简单rails教程。

特别是search的主题,请注意您方法的最后四行......

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

因此,无论您的搜索结果如何,您都应该使用@quotationscreated_at顺序中的所有引号替换category

答案 1 :(得分: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

您需要将逻辑分成上述操作以及“引用”的位置。动作是为了创建一个新的报价和&#39; seach_quotation&#39;用于搜索所有引用,它应该返回js响应,因为我们在渲染部分&#39; _list.html.erb&#39;时需要这样做。

您的观点(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 %>

以下是显示Quotations / 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请求以获取搜索结果并返回&#39; js&#39;响应。

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