【问题标题】:ActionView::MissingTemplate :: Missing template - Java Script partialActionView::MissingTemplate :: 缺少模板 - Java 脚本部分
【发布时间】:2018-09-17 22:44:38
【问题描述】:

我想使用本教程将 AJAX 搜索字段添加到我的应用程序:http://www.rymcmahon.com/articles/11

除了搜索 JS 字段之外,一切都运行良好。它根本不起作用。 我的终端控制台中的错误:

ActionView::MissingTemplate(缺少模板产品/搜索结果, 应用程序/搜索结果与 {:locale=>[:en], :formats=>[:js, :html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder, :haml]

Chrome 中的 JS 控制台

rails-ujs.self-8944eaf3f9a2615ce7c830a810ed630e296633063af8bb7441d5702fbe3ea597.js?body=1:189 得到 http://localhost:3000/products?utf8=%E2%9C%93&search=PKP&commit=search 500(内部服务器错误)

我有 turbolinks v.5 和 rails 5.1.6。

gem 'turbolinks', '~> 5'

gem 'jquery-rails'

gem 'rails', '~> 5.1.6'

好像找不到这个文件:

/products/_search-results.js.rb

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

我应该为此添加一些路线吗?感谢您的建议。

以防万一我会添加其他文件:

我的索引方法:

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)

我会在这里放其他文件。

_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

和 _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"

【问题讨论】:

    标签: javascript jquery ruby-on-rails ajax ruby-on-rails-5


    【解决方案1】:

    错误提示/products/search-results.js.rb 找不到,这是真的。你的文件名是/products/_search-results.js.rb

    当您在控制器中调用render 'search-results' 时,它将查找文件名search-results - 当您调用partial =&gt;'results' 时,它将查找文件名_results

    【讨论】:

    • 确实如此。我之前更改了它以检查它是否会以这种方式工作。 format.js {render partial: "search-results"} 返回:ActionView::MissingTemplate(缺少部分产品/_search-results, application/_search-results with {:locale=>[ :en], :formats=>[:js, :html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, :jbuilder, :haml
    • 请记住使用部分文件名时的规则,您的文件名应以_ 开头。我检查了教程,他们的代码看起来像这样render partial: 'search-results' 看到渲染后的部分关键字,你的代码中缺少它。如果添加,那么您的文件名可以是 _search-results
    • 好的。我发现了这个错误。应该有 js.erb 类型而不是 js.rb。现在我应该向博客所有者发送有关该语法的消息(我会的)。感谢您的帮助。
    【解决方案2】:

    问题在于 JS 文件的类型错误。它应该是 _search-results.js.erb 而不是 _search-results.js.rb

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多