我在发帖之前在 Stack 中进行了搜索,因为我看到了很多结果都出现了相同的错误。但是,即使我尝试了所有修复,也没有任何效果。我还删除了整个项目 3 次,并尝试做不同的事情。此时我已经完全按照 Ruby on Rails 指南中的文档(如下)所述进行操作。一旦我更改了 routes.rb 中的根目录,一切都会因未初始化的常量 StaticPages 错误而停止。
这是我在“routes.rb”中的代码
Rails.application.routes.draw do
get 'static_pages/index'
root 'static_pages/#index'
end
我用来生成控制器的命令
$ rails generate controller StaticPages index
我完全按照原样一步一步来。而且没有办法工作。我不知道该怎么做。
static_pages_index_path GET /static_pages/index(.:format) static_pages
#indexroot_path GET / static_pages/#index
rails_service_blob_path GET /rails/active_storage/blobs/:signed_id/*filename(.:format)
active_storage/blobs#showrails_blob_representation_path GET /rails/active_storage/representations/:signed_blob_id/:variation_key/*filename(.:format)
active_storage/representations#showrails_disk_service_path GET /rails/active_storage/disk/:encoded_key/*filename(.:format) active_storage/disk#show
update_rails_disk_service_path PUT /rails/active_storage/disk/:encoded_token(.:format) active_storage/disk#update
rails_direct_uploads_path POST /rails/active_storage/direct_uploads(.:format)
active_storage/direct_uploads#create
http://guides.rubyonrails.org/getting_started.html#creating-the-blog-application
通常,由于模块名和文件名不匹配,在尝试查找控制器时会出现“未初始化的常量”错误,因为 Rails 会尝试根据控制器的名称自动加载控制器。检查以下内容:
app/controllers/static_pages_controller.rb
class StaticPagesController < ApplicationController
另一个潜在问题是路由文件中的语法不正确。您不应/
在root
定义中包含 a 。尝试将其更改为:
root to: 'static_pages#index'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句