Q:

Rails - Returning a string value in collection_select

Hello StackOverflow :) ,
I have a question about a Collection_Select in a form. 
First, the collection select is a filter in a Searchform and the display of the select is working, the problem is the value passed to the request.
Before I had a text_field_tag which was working fine:
  <% text_field_tag :book_name, params[:book_name], placeholder: 'Book' %> #Text field Works

Return the param in the console  "Book_name"=>"Harry Potter"

Second choice with the collection Select :
 <%game = Book.find_by name: params[:book_name] %>
 <% collection_select(:book, :book_name, Book.all, :id, :name, {selected: book.try(:id)}, prompt: true) %>

Return this in the console : "book"=>{"book_name"=>"2"} and I would
  like to return just the same as above

"book_name" => "Name"

Here is the Search controller, maybe should I change this one ?
def search
    if params[:book_name].present?
      @searchs = User.joins(:books).where(books: {name: params[:books_name]}).near(params[:city], params[:distance] || 10)

     elsif params[:city].present?
        @searchs = User.near(params[:city], params[:distance] || 10).where("id != ?", current_user.id)

    else
      @searchs = User.none

I hope someone could help me on that. I tried many different ways but nothing worked ! Many Thanks in advance !

A:

you can do both things. 
If you prefer to pass the book name you should use
<% f.collection_select(:book, :book_name, Book.all, :name, :name, your_options = {}, your_html_options = {} %>

(note the double :name, the first one is for the option value and the second one is for the option text)
But, let me say, performing a search by name is not the best practice. querying by string is extremely slower than using an id. So, if I were you, I'll keep the collection_select as is and I'll change the search using the id like this:
      @searchs = User.joins(:books).where(books: {id: params[:books_id]}).near(params[:city], params[:distance] || 10)

or even
@searchs = User.joins(:books).where('book_id = ?', params[:book_id]).near(params[:city], params[:distance] || 10)

If you have more doubts, take a look the collection_select documentation

