gpt4 book ai didi

javascript - gem 'gon' - Quotes#index 中的名称错误,未初始化的常量 ActionView::CompiledTemplates::Gon,Rails 5

转载 作者:太空宇宙 更新时间:2023-11-04 15:35:20 34 4
gpt4 key购买 nike

我正在尝试使用 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

知道问题可能是什么,或者有任何关于替代 gem 的建议,以便更轻松地将 Controller 变量传递到 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 部分;

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

最佳答案

我想帮助您解决这个问题,因为我也在 Rails 5 中使用 gon 并且它正在工作

  • 运行捆绑安装后,
  • 打开app/views/layouts/application.html.erb并放在标签 body <%= Gon::Base.render_data %>

打开 Controller 并创建 gon 数据进行一些测试(例如下面我为您提供了 10 个用户数据

gon.users = User.limit(10)

打开你的咖啡并使用日志进行测试

alert gon.users

并且不要忘记重新启动 Rails 服务器命令

关于javascript - gem 'gon' - Quotes#index 中的名称错误,未初始化的常量 ActionView::CompiledTemplates::Gon,Rails 5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44415777/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com