简体   繁体   English

无法自动加载常量 Chart,需要 chart.rb 来定义它

[英]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.

相关问题 无法自动加载常量 POST,需要 /example/app/models/post.rb 来定义它 - Unable to autoload constant POST, expected /example/app/models/post.rb to define it Grape :: API - 无法自动加载常量Base,预期/app/api/v1/base.rb来定义它(LoadError) - Grape::API – Unable to autoload constant Base, expected /app/api/v1/base.rb to define it (LoadError) 获取“无法自动加载常量”..“预期 app/lib/subfolder/module.rb 来定义它” - Getting "Unable to autoload constant" .. '"expected app/lib/subfolder/module.rb to define it" 无法自动加载常量拍卖,需要 /app/models/configuration/auction.rb 来定义它 - Unable to autoload constant Auction, expected /app/models/configuration/auction.rb to define it 无法自动加载恒定的Role,expected role.rb以在Rails Cancan基于角色的授权中定义它 - Unable to autoload constant Role,expected role.rb to define it issue in rails cancan role based authorization 无法自动加载常量 AuthenticateFromToken,需要 ./app/api/authenticate_from_token.rb 来定义它 - Unable to autoload constant AuthenticateFromToken, expected ./app/api/authenticate_from_token.rb to define it 无法自动加载常量用户,需要/app/serializers/users.rb进行定义 - Unable to autoload constant Users, expected /app/serializers/users.rb to define it 无法自动加载常量Featured_image,应使用C:/Sites/pickyourchoice/app/models/featured_image.rb进行定义 - Unable to autoload constant Featured_image, expected C:/Sites/pickyourchoice/app/models/featured_image.rb to define it 无法自动加载常量JWTBlacklist,需要使用/home/sourabh/dev/celebration/app/models/jwt_blacklist.rb进行定义(LoadError) - Unable to autoload constant JWTBlacklist, expected /home/sourabh/dev/celebration/app/models/jwt_blacklist.rb to define it (LoadError) 无法自动加载常量SetTitle /controllers/concerns/set_title.rb进行定义 - Unable to autoload constant SetTitle /controllers/concerns/set_title.rb to define it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM