Javascript gem&x27;gon';-引号#索引中的名称错误,未初始化的常量ActionView::CompiledTemplates::Gon,Rails 5

Javascript gem&x27;gon';-引号#索引中的名称错误,未初始化的常量ActionView::CompiledTemplates::Gon,Rails 5,javascript,ruby-on-rails,rubygems,Javascript,Ruby On Rails,Rubygems,我试图使用gem'gon',但遇到上述错误,声明: namer error in Quotes#index Showing /Users/jamesbkemp/Code/QuoteEngine/app/views/layouts/application.html.erb where line #5 raised: 未初始化的常量ActionView::CompiledTemplates::Gon 你知道问题可能是什么吗?或者你对其他gems有什么建议,以使将控制器变量传递到javascri

我试图使用gem'gon',但遇到上述错误,声明:

namer error in Quotes#index

 Showing /Users/jamesbkemp/Code/QuoteEngine/app/views/layouts/application.html.erb where line #5 raised:
未初始化的常量ActionView::CompiledTemplates::Gon

你知道问题可能是什么吗?或者你对其他gems有什么建议,以使将控制器变量传递到javascript更容易

谢谢

application.html.erb,注释第5行

    <!DOCTYPE html>
<html>
  <head>
    <title>QuoteEngine</title>
    <%= Gon::Base.render_data %>    
    <%= csrf_meta_tags %>
    <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
    <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
  </head>

  <body>

    <%= render "layouts/navbar" %>

        <% flash.each do |key, value| %>
       <div class="alert  alert-info alert-<%= key %>"><%= value %></div>
    <% end %>

        <%= yield %>

  </body>
</html>

报价引擎
quotes\u controller.rb

class QuotesController < ApplicationController
  before_action :authenticate_user!, only: [ :new, :create, :edit, :update, :destroy ]
  # before_action :owners_only, only: [ :show, :edit, :index, :update, :destroy ]

  def new
    @quote = Quote.new
  end

  def create
    # @quote = Quote.new(quote_params)
    @quote = current_user.quotes.new(quote_params)
    if @quote.save
        redirect_to quote_url(@quote), notice: 'Quote request created'
    else
      render :new
    end
  end

  def show
    # @quote = Quote.find(params[:id])
    @quote = current_user.quotes.find(params[:id])
    gon.gon_quote_id = @quote.id
  end

  def index
    @quotes = current_user.quotes.all
  end

  def edit
    @quote = current_user.quotes.find(params[:id])
  end

  def update
    @quote = current_user.quotes.find(params[:id])
    if @quote.update_attributes(quote_params)
      redirect_to quote_path(@quote)
    else
      render :edit
    end
  end

  def destroy
    @quote = current_user.quotes.find(params[:id])
    @quote.destroy
    redirect_to quotes_path
  end

private

  def quote_params
    params.require(:quote).permit(:gla, :prev_cover, :co_name, :co_number, :postcode, :industry, :lives_overseas, 
                                  :scheme_start_date, :payment_frequency, :commission_level)
  end
end
class QuotesController
_quote.rb部分

    <% @quote = local_assigns[:quote] %>
<section id="quotes">
    <div class="container">
    <div class="row">
        <div class="col-md-4 text-center">
        <div class="panel panel-success panel-quote link-panel">
          <div class="panel-heading">
            <strong>GLA</strong>
            </div>
          <div class="panel-body text-center">
             <p><strong>Quote ID; <%= @quote.id %></strong></p>
          </div>

          <table class="table table-bordered">
            <tr>
              <td>Company name</td>
              <td><%= @quote.co_name %></td>
            </tr>
            <tr>
              <td>Company number</td>
              <td><%= @quote.co_number %></td>
            </tr>
            <tr>
              <td>Office postcode</td>
              <td><%= @quote.postcode %></td>
            </tr>
            <tr>
              <td>Industry</td>
              <td><%= @quote.industry %></td>
            </tr>
            <tr>
              <td>Previous cover</td>
              <td><%= @quote.prev_cover %></td>
            </tr>
            <tr>
              <td>Lives overseas</td>
              <td><%= @quote.lives_overseas %></td>
            </tr>
            <tr>
              <td>Scheme start date</td>
              <td><%= @quote.scheme_start_date %></td>
            </tr>
            <tr>
              <td>Payment frequency</td>
              <td><%= @quote.payment_frequency %></td>
            </tr>
            <tr>
              <td>Commission level</td>
              <td><%= @quote.commission_level %></td>
            </tr>
          </table>
        </div>
      </div>
    </div>
  </div>
</section>

GLA
报价ID

公司名称 公司编号 办公室邮政编码 工业 前封面 海外生活 计划开始日期 支付频率 佣金级别
index.html.erb

<% @quotes.each do |quote| %>
  <%= render :partial => "quote", locals: {quote: quote} %>
<% end %>

“quote”,本地语言:{quote:quote}%>

我想帮助您解决这个问题,因为我在rails 5中也使用了gon,而且它很有效

  • 运行包安装后
  • 打开app/views/layouts/application.html.erb 并放在标签主体下
使用打开控制器并创建gon数据进行一些测试(例如,下面我向您提供了10个用户的数据)

gon.users = User.limit(10)
打开你的咖啡,用日志进行测试

alert gon.users

别忘了重新启动rails服务器命令

你在Github上有你的代码吗,或者你能发布与此错误相关的视图和控制器代码吗?谢谢#Wijajajayd。非常奇怪,我现在在另一台mac上运行该应用程序,并从bosy not head中的
Gon:::Base::render_data
开始,它工作得很好,然后我继续按照文档中的建议,将线路重新连接到头部,并且运行良好。奇怪的是,明天早上我将在原始环境中运行,看看是否再次出现相同的问题。如果您已经正确地遵循指南,最常见的问题就是重新启动rails服务器