2
votes

I have two main models in my app, "Business" and "Category". I currently have a search text_field that works just fine, but I am also trying to implement the ability for users to search for Businesses that 'belongs_to' a certain category based on a collection_select containing the available categories.

I have the collection_select working, but I can not seem to figure out how to get it to show the businesses that 'belongs_to' the chosen category.

Here is the search info in my Business Controller:

  def search
    if params[:search].present?
      @businesses = Business.search(params[:search])
    else
      @businesses = Business.all.paginate(page: params[:page], per_page: 6)
    end
  end

My search form:

<%= form_tag search_businesses_path, method: :get, role: "search", :class => "search" do %>
    <%= label :category, "Search By Business Name" %>
    <%= text_field_tag :search, params[:search], id: "search", :placeholder => 'Search Business' %>
    <%= button_tag(type: 'submit', class: "button") do %>
        <i class="fa fa-arrow-right"></i>
    <% end %>
<% end %>

And my current category collection select:

<%= form_for :category do |f| %>
    <%= f.label :category, "Search By Category" %><br>
    <%= f.collection_select :category_id, Category.all.order('name ASC'), :id, :name %>
    <%= button_tag(type: 'submit', class: "button") do %>
        <i class="fa fa-arrow-right"></i>
    <% end %>
<% end %>

I know that I need to tell the collection select that it needs to display the businesses, but I can't seem to figure out how to properly implement it.

UPDATE

I tried the following code and no results appear:

My Business Model

def search_data
    {
      name: name,
      category_id: category_id
    }
end

My Business Controller

def search
    if params[:search].present?
      @businesses = Business.search "apples", where: { category_id: params[:category_id] }
    else
      @businesses = Business.all.paginate(page: params[:page], per_page: 6)
    end
  end

My Form

<%= form_tag search_businesses_path, method: :get, role: "search", :class => "search" do %>
    <%= label :category, "Search By Category" %>
    <%= collection_select :search, params[:category_id], Category.all.order('name ASC'), :id, :name %>
    <%= button_tag(type: 'submit', class: "button") do %>
        <i class="fa fa-arrow-right"></i>
    <% end %>
<% end %>
1
Did you check the params? Does params[:category_id] exists? Or it comes inside params[:search]? Just check and post your params which you receive. - Deepesh
So you are saying change params to <%= collection_select :search, params[:search], Category.all.order('name ASC'), :id, :name %> This does not show any results either. - ZMoodie
After playing around I was able to mostly make the correct businesses appear, but some of the categories show the correct business at the top, and then list a couple of other businesses below that. The only way I can make this happen is to keep Business.search(params[:search]) the way it is and display my form like this <%= collection_select :search, params[:category_id], Category.all.order('name ASC'), :id, :name %> otherwise nothing will show up. - ZMoodie
I guess you are not clear with form and params. You are moving in the right direction but a little knowledge of this will help you reach the target. Read about the params and how they are created. - Deepesh
Yeah, I am fairly new to rails. This is my first real project other than book tutorials. Thanks for the insight. I will read up on it. - ZMoodie

1 Answers

0
votes

Will this work for you:

In the Business model define this method:

def search_data
  {
    name: name,
    category_id: category_id
  }
end

Or I think you can write this too(not sure): category: category.name

So this will index the category too and you can search using that then in your controller like this:

Business.search "apples", where: { category_id: params[:category_id] }

or if the category.name works then { category: params[:category] }

Change the params according to your form.