首页 > 解决方案 > NameError: 未初始化的常量GoalsController::Goal ruby​​-on-rails

问题描述

我正在制作一个网站,它将跟踪用户的目标。我目前在尝试创建模型对象以将表单字段绑定到时遇到 NameError。

当我在大多数情况下查找此错误时,情况是相同的,但是他们使对象复数(又名@goal =Goals.new),而这里的情况并非如此。我的另一个侧面查询是在 form_for 标记中,通常指定一个 url。在我的情况下,我通常会将其设为目标#create(POST 请求)的路径,但我的路由没有显示此 url 的帮助程序/路径。提前致谢。

这是控制器的goals_controller.rb:

class GoalsController < ApplicationController
  # GET request to /goals/new
  def new
    @goal = Goal.new
  end

  # GET request to /goals
  def index

  end
end

完整错误: Completed 500 Internal Server Error in 2ms (ActiveRecord: 0.0ms)

NameError (uninitialized constant GoalsController::Goal):

app/controllers/goals_controller.rb:4:in `new'
  Rendering /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout
  Rendering /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb
  Rendered /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (2.3ms)
  Rendering /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb
  Rendered /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.2ms)
  Rendering /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb
  Rendered /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms)
  Rendered /home/ec2-user/.rvm/rubies/ruby-2.6.3/lib/ruby/gems/2.6.0/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/diagnostics.html.erb within rescues/layout (14.8ms)

标签: ruby-on-railsrubycontrollernameerror

解决方案


推荐阅读