I am fairly new to Ruby on Rails, and I am trying to implement an advanced search. The advanced search is to search for books and their categories.
Please let me know if you need additional information.
I have implemented the advanced search form view, new view and show view.
But when I try to search for books, it doesn't display the search results neither doesn't render the show view for searches. It rather renders a new view page for Advanced Search.
Here is my Searches Controller Code
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
Here is my Search Model Code
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
Here is my Searches _form.html.erb Code
<%= 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 %>
Here is my Searches new.html.erb Code
<% 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>
Here is my Searches show.html.erb Code <% content_for :title, "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>
I need some assistance on how my search results can be displayed on the show view of the searches, instead of the Advanced Search page re-rendering the a new search page each time I click the Search Button of the Advanced Search. I need a way to connect the Show View of the Advanced Search to the New Advanced Search Page, so that it can display search results.
Thank you.
I was able to fix it.
On the 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>
I simply changed the form tag to to a div tag, and also removed the action attribute from the form tag.
I have learnt a lot in the process.
That's all.
I hope this helps