宝石' gon' - 引号中的名称错误#index,未初始化的常量ActionView :: CompiledTemplates :: Gon,Rails 5

时间:2017-06-07 14:40:41

标签: javascript ruby-on-rails rubygems

我正在尝试使用宝石' gon'但遇到上述错误,说明;

namer error in Quotes#index

 Showing /Users/jamesbkemp/Code/QuoteEngine/app/views/layouts/application.html.erb where line #5 raised:

未初始化的常量ActionView :: CompiledTemplates :: Gon

任何想法可能是什么问题和/或任何关于替代宝石的建议使控制器变量更容易传递到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_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

_quote.rb partial;

    <% @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>

index.html.erb

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

1 个答案:

答案 0 :(得分:1)

我想帮助你处理你的情况,因为我还在轨道5中使用gon并且它正在工作

  • 运行捆绑安装后,
  • 打开app / views / layouts / application.html.erb 并置于标签主体&lt;%= Gon :: Base.render_data%&gt;

打开您的控制器进行一些测试并创建gon数据(例如,下面我给了您10个用户数据

gon.users = User.limit(10)

打开咖啡,用日志测试

alert gon.users

并且不要忘记重启rails服务器命令