2

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 %>
ZMoodie
  • 41
  • 5
  • 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 Mar 21 '16 at 12:58
  • 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 Mar 21 '16 at 13:10
  • 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 Mar 21 '16 at 13:34
  • 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 Mar 21 '16 at 13:41
  • 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 Mar 21 '16 at 14:51
  • Okay. So see how the `params` are built from the `view` and then use them accordingly in `controller`. You never use `params` in the view in `collection_select`. Add to check the `params` in controller either you can see in the logs or you can see by adding `byebug` in your `action`. – Deepesh Mar 21 '16 at 14:57
  • I have posted similar question like this. Please see as I need help with the same issue http://stackoverflow.com/questions/42155780/how-to-make-advanced-search-using-searchkick –  Feb 10 '17 at 10:03

1 Answers1

0

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.

Deepesh
  • 6,138
  • 1
  • 24
  • 41
  • I have tried your suggestions in several different ways, but either no results appear, or a few seemingly random ones appear. – ZMoodie Mar 21 '16 at 01:33