Search code examples
ruby-on-railssolrfull-text-searchnavbarsunspot

How can I declare path for bootstrap navbar search form using rails


I want to search textbooks in landing page but it is not working. I had to go /textbooks page and then I can search. Also, I want to fulltext search. I am using sunspot & solr. But full text search not working. If I have a textbook title called "Algorithm". If I search "algo" I want to be searchable. Can you help?

I have codes below.

views/layouts:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>
    <% if content_for? :title %>
        <%= yield :title %>
    <% else %>
        Udeal
    <% end %>
  </title>
  <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track' => true %>
  <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>

  <%= csrf_meta_tags %>
</head>

<body>

<%= render 'partials/navbar' %>

<%= render 'partials/flash_notification' %>

<%= yield %>

</body>

</html>

config/routes.rb:

Rails.application.routes.draw do
  root 'home#index'
  get '/about', to: 'home#about'

  resources :textbooks
  get '/textbooks/search', to: 'textbooks#search'
  devise_for :users

_navbar.html.erb:

<form class="navbar-form navbar-left" role="search">
        <div class="form-group">
          <%= form_tag(textbooks_path, :method => "get", id: "search-form", html: {class: 'form-group'}) do %>
            <%= text_field_tag :search, params[:search], placeholder: "Search all deals", class: "form-control" %>
        </div>
            <%= submit_tag "Search", :class => 'btn btn-default',:title => nil%>
          <% end %>
      </form>

my TextbooksController:

class TextbooksController < ApplicationController
  before_action :set_textbook, only: [:show, :edit, :update, :destroy]

  def search
    @search = Textbook.search do
      keywords(params[:q])
    end
  end

  def index
    @textbooks = Textbook.all
    @search = Textbook.search do
      fulltext params[:search]
      #keywords params[:search]
    end
    @textbooks = @search.results
  end

Textbook model:

class Textbook < ActiveRecord::Base
    belongs_to :user

    searchable do
        text :title, :default_boost => 2
        text :subject
    end

below is my index.html.erb for Textbook page

<div>
          <%= form_tag textbooks_path, :method => :get do %>
            <p>
            <a style="color:#000000" title="Suggest!" data-toggle="popover" data-trigger="hover" data-content="Title or Subject">
              <%= text_field_tag :search, params[:search] %>
            </a>
            <%= submit_tag "Search", :title => nil, class: "btn btn-success btn-sm" %>
          <% end %>
        </div>

EDIT:

I typed rake routes:

                  Prefix Verb   URI Pattern                    Controller#Action
                    root GET    /                              home#index
                   about GET    /about(.:format)               home#about
        search_textbooks GET    /textbooks/search(.:format)    textbooks#search
               textbooks GET    /textbooks(.:format)           textbooks#index
                         POST   /textbooks(.:format)           textbooks#create
            new_textbook GET    /textbooks/new(.:format)       textbooks#new
           edit_textbook GET    /textbooks/:id/edit(.:format)  textbooks#edit
                textbook GET    /textbooks/:id(.:format)       textbooks#show
                         PATCH  /textbooks/:id(.:format)       textbooks#update
                         PUT    /textbooks/:id(.:format)       textbooks#update
                         DELETE /textbooks/:id(.:format)       textbooks#destroy
        new_user_session GET    /users/sign_in(.:format)       devise/sessions#new
            user_session POST   /users/sign_in(.:format)       devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)      devise/sessions#destroy
           user_password POST   /users/password(.:format)      devise/passwords#create
       new_user_password GET    /users/password/new(.:format)  devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format) devise/passwords#edit
                         PATCH  /users/password(.:format)      devise/passwords#update
                         PUT    /users/password(.:format)      devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)        devise/registrations#cancel
       user_registration POST   /users(.:format)               devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)       devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)          devise/registrations#edit
                         PATCH  /users(.:format)               devise/registrations#update
                         PUT    /users(.:format)               devise/registrations#update
                         DELETE /users(.:format)               devise/registrations#destroy

Solution

  • _navbar.html.erb would be:

    <form class="navbar-form navbar-left" role="search">
            <div class="form-group">
              <%= form_tag(search_textbooks_path, :method => "get", html: {class: 'form-group', id: "search-form"}) do %>
                <%= text_field_tag :search, params[:search], placeholder: "Search all deals", class: "form-control" %>
            </div>
                <%= submit_tag "Search", :class => 'btn btn-default',:title => nil%>
              <% end %>
          </form>
    

    Previously your form url was wrong. If you still face the same problem, then post your log errors.

    Your search method would be:

    def search
        @search = Textbook.search do
           fulltext params[:search]
        end
        @textbooks = @search.results
      end