gpt4 book ai didi

ruby-on-rails - 在 Rails 应用程序中获取 "uninitialized constant ApplicationController"

转载 作者:数据小太阳 更新时间:2023-10-29 09:01:53 25 4
gpt4 key购买 nike

我正在尝试学习Rails 4 框架并关注官方doc

我运行了以下命令:

rails new fold

它创建了一个新项目

rails generate controller home index

它生成一个home_controllerviews/home/index.html.erb

home_controller 的内容:

class HomeController < ApplicationController
def index
end
end

对于路由,我在 routes.rb

中添加了以下代码
 get 'home/index' => 'home#index'

运行应用程序后我得到:

uninitialized constant ApplicationController

我在这里做错了什么?这是 Stacktrace

app/controllers/home_controller.rb:1:in `<top (required)>'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:261:in `const_get'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:261:in `block in constantize'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `each'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `inject'
activesupport (4.2.4) lib/active_support/inflector/methods.rb:259:in `constantize'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:72:in `controller_reference'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:62:in `controller'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:41:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'

代码有什么问题?

最佳答案

您有application.rb 文件吗?此文件应位于 app/controllers 内。所有其他 Controller 都继承自该 Controller 。最简单的设置如下所示:

class ApplicationController < ActionController::Base
end

关于ruby-on-rails - 在 Rails 应用程序中获取 "uninitialized constant ApplicationController",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33449357/

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