高级搜索框不显示结果或连接到显示视图

Advanced Search Box Not Showing Results or Connecting to the Show View

我对 Rails 上的 Ruby 还很陌生,我正在尝试实施高级搜索。高级搜索是搜索书籍及其类别。

如果您需要更多信息,请告诉我。

我已经实现了高级搜索表单视图、新视图和显示视图。

但是当我尝试搜索书籍时,它既不显示搜索结果也不呈现搜索的显示视图。它而是为高级搜索呈现一个新的视图页面。

这是我的搜索控制器代码

class SearchesController < ApplicationController
  def new
    @search = Search.new
  end

  def create
    @search = Search.create(search_params)
    redirect_to @search
  end

  def show
    @search = Search.find(params[:id])
  end

  private

  def search_params
    params.require(:search).permit(:keywords, :category)
  end
end

这是我的搜索模型代码

class Search < ApplicationRecord
  def search_books

    books = Book.all

    books = books.where(["name LIKE ?","%#{keywords}%"]) if 
      keywords.present?
    books = books.where(["category LIKE ?",category]) if 
      category.present?

 return books

 end
end

这是我的搜索结果_form.html.erb代码

<%= form_with(model: search, local: true) do |form| %>
   <div class="form-group">
      <%= form.text_field :keywords, {placeholder: 'Keywords', 
      :class => 'form-control pb_height-50 reverse'} %>
  </div>

   <div class="form-group">
      <%= form.text_field :category, {placeholder: 'Category', 
      :class => 'form-control pb_height-50 reverse'} %>
   </div>

   <div class="form-group">
      <%= form.submit :class => 'btn btn-primary btn-lg btn-block 
       pb_btn-pill  btn-shadow-blue', value: 'Search'%>
   </div>

<% end %>

这是我的搜索结果new.html.erb代码

<% content_for :title, "New Search" %>

 <section class="pb_cover_v3 overflow-hidden cover-bg-indigo cover- 
    bg-opacity text-left pb_gradient_v1 pb_slant-light menu-section" 
    id="section-home">

 </section>

 <section class="pb_section bg-light pb_slant-white pb_pb-250" 
   id="section-features">
  <div class="container">
    <div class="row align-items-center justify-content-center">
      <div class="col-md-12 relative align-self-center">

        <form action="/search" class="bg-white rounded pb_form_v1">
          <h2 class="mb-4 mt-0 text-center">Advanced Search</h2>
          <hr>

          <%= render 'form', search: @search %>

          <hr>
          <%= link_to 'Back', books_path %>
        </form>

       </div> 
      </div>
    </div>
 </section>

这是我的搜索 show.html.erb 代码 <% content_for :标题, "Search Result" %>

   <section class="pb_cover_v3 overflow-hidden cover-bg-indigo 
     cover-bg-opacity text-left pb_gradient_v1 pb_slant-light menu- 
     section" id="section-home">

   </section>

  <section class="pb_section pb_slant-white pb_pb-250" id="section- 
  features">

    <div class="container">
      <div class="row titlerow">
        <h1><%= Search Result %></h1>
          <p><%= link_to 'Back', new_search_path %></p>

            <% if @search.search_books.empty? %>

              <p>No Records Found</p>

            <% else%>

               <% @search.search_books.each do |c| %>
                <br>

               <div class="div">
               <h1><%= c.name %></h1>
               <p>Category: <%=c.category %></p>

              </div>

             <% end %>

           <% end %>      
          </div>
        </div>
 </section>

我需要一些帮助,了解我的搜索结果如何显示在搜索的显示视图上,而不是高级搜索页面 re-rendering 每次单击搜索按钮时都会出现一个新的搜索页面高级搜索。我需要一种方法将高级搜索的显示视图连接到新的高级搜索页面,以便它可以显示搜索结果。

谢谢。

我能够修复它。

在new.html.erb

<section class="pb_section bg-light pb_slant-white pb_pb-250" id="section-features">
      <div class="container">
        <div class="row align-items-center justify-content-center">
          <div class="col-md-12 relative align-self-center">

            <form action="/search" class="bg-white rounded pb_form_v1">
              <h2 class="mb-4 mt-0 text-center">Advanced Search</h2>
              <hr>

              <%= render 'form', search: @search %>

              <hr>
              <%= link_to 'Back', books_path %>
            </form>

          </div> 
        </div>
      </div>
</section>

我只是将 form 标签更改为 div 标签,并删除了 表单标签中的 action 属性。

我在这个过程中学到了很多。

就这些了。

希望对您有所帮助