Search code examples
sqlruby-on-railssearchruby-on-rails-5ransack

Layering navbar simple search with Ransack for further scoping


I have a search bar on my navigation header where users can search for the titles of articles. This will route them to the articles/browse.html.erb page where they can further scope the results down by categories and name of authors.

I have followed this tutorial to do the navbar search. I have attempted to use Ransack to allow further filtering on the browse page. However, i'm having diffculty combining the 2 search methods with the following issues:

1) The navbar search works fine and returns the correct results, but when i filter the results further again by category name, i get EDIT 0 results ALL the results.

2) When i do the initial search on the browse page itself with the article title, ransack works. However, Ransack doesn't seem to work when i further scope the results down by category/author name. (EDIT: ransack works for filter by category with the below edit in browse.html.erb, but not for author name)

browse.html.erb

  <%= search_form_for @q, url: browse_articles_path, method: :get do |f| %>
    <div class="form-group">
      <div class = "row">
        <div class="col-md-12 col-sm-12 col-xs-12">
          <%= f.search_field :title_cont, placeholder: "Find Articles", class: "form-control" %>
        </div>
      </div>
    </div>
    <div class="form-group">
      <div class ="row">
        <div class="col-md-6 col-sm-6 col-xs-6">
          <%= f.select :categories_name_cont, options_from_collection_for_select(Category.all, //EDIT change "id" to// "name", "name", @q.categories_name_eq), 
                                                                                { :prompt => "Any Category" },
                                                                                { class: "form-control" } %>
        </div>
        <div class="col-md-6 col-sm-6 col-xs-6">
          <%= f.search_field :users_name_cont, class: "form-control", placeholder: "Author" %>
        </div>
      </div>
    </div>
    <div class="form-group">
      <%= f.submit class: 'btn btn-primary btn-md' %>
    </div>
  <% end %>

  <% if params[:search] %>
    <%= render @articles %>
  <% elsif params[:q] %>
    <%= render @articles_morefilter %>
  <% else %>
    <%= render @articles %>
  <% end %>

articles_controller.rb

  def browse
    if params[:search]
      @articles = Article.search(params[:search])
      @categories = Category.joins(:articles).where('articles.title LIKE ?', "%#{params[:search]}%").uniq
    else
      @articles = Article.published.order("cached_votes_up DESC")
      @categories = Category.with_articles.order('name ASC').all
    end

      @q = Article.ransack(params[:q])
      @articles_morefilter = @q.result.includes(:categories, :users)
  end

EDIT model: article.rb

class Article < ApplicationRecord
  belongs_to :user
  has_many :article_categories
  has_many :categories, through: :article_categories

  def self.search(search)
      where('title LIKE ?', "%#{search}%")
  end
end

routes.rb

  resources :articles do
    collection do
      get 'browse'
    end
  end

schema:

  create_table "articles", force: :cascade do |t|
    t.string "title"
    t.text "description"
    t.bigint "user_id"
  end

  create_table "users", force: :cascade do |t|
    t.string "name"
  end

_navigation.html.erb

<%= form_tag(browse_articles_path, :method => 'get', :id => "articles_search") do %>
  <div class="form-group">
    <%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %>
  </div>
  <button type="submit" class="btn btn-default">Search</button>
<% end %>

Solution

  • you should be able to simply search all the fields with ransack. (based on current browse.html.erb)

    def browse
       @q = Article.ransack(params[:q])
       @articles = @q.result.includes(:categories, :users)
    end