ruby-on-rails – rails app’ininitialized constant HomeController’中的路由错误

我是使用基础的新手,我使用脚手架创建了简单的帖子应用程序
我已经完成了以下步骤:

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

现在app运行良好@本地主机端口3000 /帖子

但是当我点击导航栏中的“主页”按钮时,它会产生错误

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”的控制器,其中包含一个动作“index”.

为此,您需要在app / controllers文件夹中有一个HomeController.rb,并在其中有一个def为’index’.

我建议运行此命令

rails generate controller home index

生成家庭控制器.在运行scaffold命令之前,许多教程都会为您提供此行.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...