ActionView::MissingTemplate :: Missing template - Java Script partial

Viewed 1258

I want to add AJAX search field to my app using this tutorial: http://www.rymcmahon.com/articles/11

Everything works well but the search JS field. It do not work at all. Errors in my terminal console:

ActionView::MissingTemplate (Missing template products/search-results, application/search-results with {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder, :haml]

JS console in Chrome

rails-ujs.self-8944eaf3f9a2615ce7c830a810ed630e296633063af8bb7441d5702fbe3ea597.js?body=1:189 GET http://localhost:3000/products?utf8=%E2%9C%93&search=PKP&commit=search 500 (Internal Server Error)

I have turbolinks v.5 and rails 5.1.6.

gem 'turbolinks', '~> 5'

gem 'jquery-rails'

gem 'rails', '~> 5.1.6'

It is like this file can not be found:

/products/_search-results.js.rb

$("#product_table").hide();
$("#search-results").html("<%= escape_javascript(render :partial =>'results') %>");

Should I add some routes for that? Thanks in advise.

Just in case I will add other files:

My index method:

class ProductsController < ApplicationController
  def index
    if params[:search]
      @search_results_products = Product.options_sortable(params)
            respond_to do |format|
                format.js {render "search-results"}
                format.html
            end
        else
            @products = Product.all.paginate(:page =>params[:page], :per_page => 20)
        end
  end.....

index.html.haml:

.container-fluid
    %h2.center
        =link_to "Create New Product", new_product_path

    =render 'search_products'

    %br
    %h1.center List of all the Products
    #product-table
        =render 'table_products'
    #search-results
    .col-lg-2.offset-md-5
        =will_paginate(@product)

I will put here other filer.

_table.products:

.col-md-8.offset-md-2
    %table.frame
        %thread
            %tr.h2
              %th
                =link_to "Name", sort: "product_name"
                %th
                  =link_to "Description", sort: "description"
                %th Order Name
                %th options
        %tbody
            -@products.each do |product|
                %tr
                  %th.h4
                    =product.product_name
                    %th
                      =product.description
                    %th
                      =product.order.name
                    -if product.order.user == current_user
                      %th
                        =link_to 'edit', edit_product_path(product)
                        ||
                        =link_to 'delete', product_path(product), 
                                           remote: true,
                                           method: :delete,
                                    data: { confirm: 'Are you sure?'}
                    -else
                        %th
                          no permission

_results.html.haml

.col-md-8.offset-md-2
    %table.frame
        %thread
          %tr.h2
            %th
              =link_to "Name", sort: "product_name"
            %th
              =link_to "Description", sort: "description"
            %th Order Name
            %th options
        %tbody
            -@search_results_products.each do |product|
              %tr
                %th.h4
                  =product.product_name
                %th
                  =product.description
                %th
                  =product.order.name
                -if product.order.user == current_user
                  %th
                    =link_to 'edit', edit_product_path(product)
                    ||
                    =link_to 'delete', product_path(product), 
                                remote: true,
                                method: :delete,
                               data: { confirm: 'Are you sure?'}
                    -else
                      %th
                        no permission

and the _search_products.html.haml

.col-lg-4.offset-md-4
  =form_tag(products_path, :method => "get", remote: true) do
    .form-group.form-control-md
      =text_field_tag :search, params[:search], placeholder: 'Enter search text'
      =submit_tag 'search', class: "btn"
2 Answers

The error says /products/search-results.js.rb can't be found which is true. Your file name is /products/_search-results.js.rb

When you call render 'search-results' in controller It will look for file name search-results - When you call partial =>'results' it will look for file name _results

The issue was in the bad type of the JS file. It should be _search-results.js.erb instead of _search-results.js.rb.

screenshoot

Related