gpt4 book ai didi

ruby-on-rails - rails 应用程序中的路由错误 'uninitialized constant HomeController'

转载 作者:行者123 更新时间:2023-12-03 16:04:46 27 4
gpt4 key购买 nike

我刚开始使用基础,我使用脚手架创建了简单的 Posts 应用程序
我已经完成了以下步骤:

rails new blog

然后将以下内容添加到 Gemfile
gem 'foundation-rails'
group :development do
gem 'rails_layout'
end

进而
$ bundle install
$ rails generate layout:install foundation5 --force
$ rails g scaffold Post title desc:text
$ rake db:migrate

现在应用程序在本地主机端口 3000/posts 上运行良好
enter image description here
但是当我点击导航栏中的“主页”按钮时,它会产生错误:
enter image description here

application.html.erb 文件:
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><%= content_for?(:title) ? yield(:title) : "Found Rails" %></title>
<meta name="description" content="<%= content_for?(:description) ? yield(:description) : "Found Rails" %>">
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
<%# Modernizr is required for Zurb Foundation %>
<%= javascript_include_tag 'vendor/modernizr' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
<%= csrf_meta_tags %>
</head>
<body>
<header>
<%= render 'layouts/navigation' %>
</header>
<main role="main">
<%= render 'layouts/messages' %>
<%= yield %>
</main>
</body>
</html>

_navigation.html.erb 文件:
<%# navigation styled for Zurb Foundation 5 %>
<nav class="top-bar" data-topbar>
<ul class="title-area">
<li class="name"><%= link_to 'Home', root_path %></li>
<li class="toggle-topbar menu-icon"><a href="#">Menu</a></li>
</ul>
<div class="top-bar-section">
<ul>
<%= render 'layouts/navigation_links' %>
</ul>
</div>
</nav>

我的 routes.rb 文件:
Rails.application.routes.draw do
resources :posts
root :to => "home#index"

# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

# You can have the root of your site routed with "root"
# root 'welcome#index'

# Example of regular route:
# get 'products/:id' => 'catalog#view'

# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase

# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products

# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end

# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end

# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end

# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable

# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end

我错过了什么?。

最佳答案

你的问题是这里的这一行:

root :to => "home#index"

在 routes.rb 文件中。

这会告诉您的应用程序根 URL(因此 http://:3000/URL)应该寻找一个名为“home”的 Controller ,其 Action 为“index”。

为此,您需要在您的 app/controllers 文件夹中有一个 HomeController.rb 并在其中包含一个用于“索引”的 def。

我建议运行此命令
rails generate controller home index

生成家庭 Controller 。许多教程会在您运行 scaffold 命令之前为您提供这一行。

关于ruby-on-rails - rails 应用程序中的路由错误 'uninitialized constant HomeController',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23465769/

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