title: Rails search form throwing back error Couldn't find Product with 'id'=search
body: <p>I'm building an rails app to learn ruby on rails.(I'm very unexperienced)
  I'm trying to implement a search bar to the app.</p>
  
  <p>When I hit search rails throws back this error <code>Couldn't find Product with 'id'=search</code></p>
  
  <p>in this line <code>
   # Use callbacks to share common setup or constraints between actions.
      def set_product
        @product = Product.find(params[:id])
      end</code></p>
  
  <p>the server log shows this </p>
  
  <pre><code>Started GET "/products/search?utf8=%E2%9C%93&amp;query=Desk" for ::1 at 2016-06-24 16:02:32 +0000
  Processing by ProductsController#show as HTML
   Parameters: {"utf8"=&gt;"✓", "query"=&gt;"Desk", "id"=&gt;"search"}
   Product Load (0.4ms)  SELECT  "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1  [["id", 0]]
  Completed 404 Not Found in 4ms (ActiveRecord: 0.4ms)
  
  ActiveRecord::RecordNotFound (Couldn't find Product with 'id'=search):
  app/controllers/products_controller.rb:77:in `set_product'
  </code></pre>
  
  <p>I've been working on this search form for hours and checked out various tutorials and stackoverflow posts trying to figure this out. 
  I'm totally lost but the solution is probably pretty simple.
  Please can anyone help me.</p>
  
  <p>this is my search form in _navbar.html.erb</p>
  
  <pre><code>    &lt;div class="input-group"&gt;
  
      &lt;%= form_tag search_products_path, method: 'get', class: 'navbar-form' do %&gt;
        &lt;%= text_field_tag :query, params[:query], placeholder: "Search", class: "form-control"%&gt;
      &lt;/div&gt;
       &lt;span class="input-group-btn"&gt;
         &lt;%= submit_tag "Search", class: 'btn btn-default' %&gt;
      &lt;/span&gt;
  
    &lt;% end %&gt;
  </code></pre>
  
  <p>This is the products.rb model</p>
  
  <pre><code>class Product &lt; ActiveRecord::Base
  
  
    mount_uploader :image, ImageUploader
  
    validates_presence_of :name, :price, :stock_quantity
    validates_numericality_of :price, :stock_quantity
  
    belongs_to :designer
    belongs_to :category
    belongs_to :page
  
    def self.search(query)
  
      where("name LIKE ? OR description LIKE ?", "%#{query}%", "%#{query}%") 
    end
  
   end
  </code></pre>
  
  <p>This is the products_controller.rb</p>
  
  <pre><code> class ProductsController &lt; ApplicationController
    before_action :set_product, only: [:show, :edit, :update, :destroy]
    before_filter :initialize_cart
    before_action :authenticate_admin!, only: [ :new, :edit, :update, :create, :destroy ]
    # GET /products
    # GET /products.json
    def index
     @products = Product.all
    end
  
    def search
  
      @products = Product.search(params[:query]).order("created_at DESC")
      @categories = Category.joins(:products).where(:products =&gt; {:id =&gt; @products.map{|x| x.id }}).distinct
  
    end
  
    # GET /products/1
    # GET /products/1.json
    def show
  
    end
  
    # GET /products/new
    def new
     @product = Product.new
    end
  
    # GET /products/1/edit
    def edit
  
    end
  
    # POST /products
    # POST /products.json
    def create
      @product = Product.new(product_params)
  
      respond_to do |format|
    if @product.save
      format.html { redirect_to @product, notice: 'Product was successfully created.' }
      format.json { render :show, status: :created, location: @product }
    else
      format.html { render :new }
      format.json { render json: @product.errors, status: :unprocessable_entity }
       end
     end
    end
  
    # PATCH/PUT /products/1
    # PATCH/PUT /products/1.json
    def update
       respond_to do |format|
         if @product.update(product_params)
           format.html { redirect_to @product, notice: 'Product was successfully updated.' }
           format.json { render :show, status: :ok, location: @product }
         else
           format.html { render :edit }
           format.json { render json: @product.errors, status: :unprocessable_entity }
         end
       end
     end
  
     # DELETE /products/1
     # DELETE /products/1.json
     def destroy
       @product.destroy
       respond_to do |format|
       format.html { redirect_to products_url, notice: 'Product was successfully destroyed.' }
       format.json { head :no_content }
     end
   end
  
   private
      # Use callbacks to share common setup or constraints between actions.
      def set_product
       @product = Product.find(params[:id])
      end
  
      # Never trust parameters from the scary internet, only allow the white list through.
     def product_params
       params.require(:product).permit(:name, :description, :price, :image, :category_id, :stock_quantity, :designer_id, :query)
     end
   end
  </code></pre>
  
  <p>this is views/products/search.html.erb</p>
  
  <pre><code> &lt;table class="table table-hover"&gt;
     &lt;thead&gt;
       &lt;tr&gt;
         &lt;th&gt;Name&lt;/th&gt;
         &lt;th&gt;Description&lt;/th&gt;
         &lt;th&gt;Designer&lt;/th&gt;
         &lt;th&gt;Price&lt;/th&gt;
         &lt;th&gt;Stock&lt;/th&gt;
         &lt;th&gt;Image&lt;/th&gt;
  
      &lt;/tr&gt;
     &lt;/thead&gt;
  
   &lt;tbody&gt;
     &lt;% @products.each do |product| %&gt;
  
       &lt;tr&gt;
          &lt;td&gt;&lt;%= link_to product.name, product %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= product.description %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= product.designer.designer_name %&gt;&lt;/td&gt;
  
          &lt;td&gt;&lt;%= number_to_currency product.price %&gt;&lt;/td&gt;
          &lt;td&gt;&lt;%= product.stock_quantity %&gt;&lt;/td&gt;
  
          &lt;td&gt;&lt;%= image_tag product.image.thumb %&gt;&lt;/td&gt;
  
  
      &lt;% end %&gt;
     &lt;/tr&gt;
   &lt;/tbody&gt;
  </code></pre>
  
  <p>and in routes.rb I have</p>
  
  <pre><code> Rails.application.routes.draw do
    post 'products/search' =&gt; 'products#search', as: 'search_products'
    get 'pages/index'
  
    get 'pages/about'
  
    get 'pages/location'
  
    get 'pages/stockists'
  
    devise_for :users
     resources :categories
     resources :categories
     resources :designers
     resources :category_names
     resources :products
  
  
     resource :cart, only: [:show] do
      post "add", path: "add/:id", on: :member
     get :checkout
    end
  
    resources :orders, only: [ :index, :show, :create, :update ] do
      member do
       get :new_payment
        post :pay
      end
    end
  
  
     root 'pages#index'
  
  end
  </code></pre>