[英]Unable to autoload constant Chart, expected chart.rb to define it
I'm getting a "Unable to autoload constant Chart, expected chart.rb to define it" error, but I do not where to start to find the problem.我收到“无法自动加载常量图表,需要 chart.rb 来定义它”错误,但我不知道从哪里开始找到问题。 I have tried many things, but nothing seems to work.
我尝试了很多东西,但似乎没有任何效果。 Any help will be very appreciated.
任何帮助将不胜感激。
Chart model图表 model
class Chart < ApplicationRecord
belongs_to :user, optional: true
end
In the code above, the:user is parent and connected to Chart.在上面的代码中,:user 是父级并连接到 Chart。
Chart Controller图表 Controller
class ChartsController < ApplicationController
before_action :set_chart, only: [:show, :edit, :update, :destroy]
# GET /charts
# GET /charts.json
def index
@charts = Chart.all
end
# GET /charts/1
# GET /charts/1.json
def show
@chart = Chart.find(params[:id])
end
# GET /charts/new
def new
@chart = Chart.new
end
# GET /charts/1/edit
def edit
end
# POST /charts
# POST /charts.json
def create
@chart = Chart.new(chart_params)
respond_to do |format|
if @chart.save
format.html { redirect_to @chart, notice: 'Chart was successfully created.' }
format.json { render :show, status: :created, location: @chart }
else
format.html { render :new }
format.json { render json: @chart.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /charts/1
# PATCH/PUT /charts/1.json
def update
respond_to do |format|
if @chart.update(chart_params)
format.html { redirect_to @chart, notice: 'Chart was successfully updated.' }
format.json { render :show, status: :ok, location: @chart }
else
format.html { render :edit }
format.json { render json: @chart.errors, status: :unprocessable_entity }
end
end
end
# DELETE /charts/1
# DELETE /charts/1.json
def destroy
@chart = Chart.find(params[:id])
if @chart.present?
@chart.destroy
end
respond_to do |format|
format.html { redirect_back(fallback_location: '/charts')}
format.json { head :no_content }
end
end
private
def chart_params
params.require(:chart).permit(~~)
end
end
The error says that there is a problem at line 22 which is该错误表明第 22 行存在问题,即
# GET /charts/new
This error happens when the model is placed in a wrong directory.当 model 放置在错误的目录中时,会发生此错误。 The path of the chart model should be
图表 model 的路径应该是
app/models/chart.rb
Move the chart model to the correct location, it will work fine.将图表 model 移动到正确的位置,它将正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.