neesop
neesop

Reputation: 49

Search User inputted string in rails

I am trying to implement a simple string search. I have a Book model which has bookname, isbn, authname as attributes. I have added this at the end of my index.html.haml to allow the user to search for a book using isbn.

=form_tag({controller: "books_controller", action: "searchbook"}, :method => :get) do
  .search_field
    =text_field_tag :q
    =button_tag 'Go' , class: 'search-button' , type: :submit
%br

Added this to my book_controller.rb

def searchbook
    @searchedbook = Book.where(isbn: q)
end

Added this to my routes file:

Rails.application.routes.draw do
  resources :courses

  resources :books

  get 'books_controller/searchbook' => 'books_controller#searchbook'
end

After running rails server when I go to http://localhost:3000/books I get a list of all the books. I can see the input box at the end of list and when I enter an isbn number to search for the book, this is the error that I get:

uninitialized constant BooksControllerController

When I run rake routes I see the route in the list:

books_controller_searchbook GET    /books_controller/searchbook(.:format) books_controller#searchbook

I am not sure what went wrong there. All I want to do is take the user inputted string and run the Book.where(isbn: "userinput") query to find the book.

Upvotes: 0

Views: 46

Answers (1)

Sonalkumar sute
Sonalkumar sute

Reputation: 2575

See what's the error uninitialized constant BooksControllerController, this means it already knowing that 'books' is a controller, but you have specified books_controller so it is becoming BooksControllerController

Actually you don't have to specify the controller string

Try this

 get 'books/searchbook' => 'books#searchbook'

Or

match 'searchbook', to: 'books#searchbook', via: :get

Upvotes: 2

Related Questions