vanderleipinto/preparation_mentorship

Filter Supplier by Author's Name

Closed this issue · 1 comments

  • Create a search form to find suppliers or insert authors field in the existing form.
  • Render the form in the suppliers index.
  • Change the Controller to select the suppliers.

Create a search form to find suppliers or insert authors field in the existing form.

Change the form inserting the field author in app/views/suppliers/_search_form.html.erb:

...
<div>
  <%= form_tag(suppliers_path, method: :get) do %>    
    
    <label for="search_author">Author:</label>    

    <%= select_tag(:search_author, options_for_select(Author.pluck(:name, :id)), prompt: "Choose an Author") %>

    <%= submit_tag("Search Author's Supplier(s)") %>
    <% end %>
</div> 

Render the form in the suppliers index.

Change the render to contains the author in app/views/suppliers/index.html.erb:

...
<%= render "search_form", supplier: @supplier, author: @author %>
...

Change the Controller to select the suppliers.

insert an elsif in the index action in app/controllers/suppliers_controller.rb

  def index
    if params[:search_supplier].present?
      @suppliers = Supplier.where("name LIKE ?","%#{params[:search_supplier]}%")
    elsif params[:search_author].present?
      @suppliers = Supplier.joins(parts: { assemblies: { books: :author } }).where(authors: { id: params[:search_author] }).distinct
    else
      @suppliers = Supplier.all
    end
  end