rails app中的路由错误'未初始化的常量HomeController'

时间:2014-05-05 05:59:53

标签: ruby-on-rails zurb-foundation rails-routing

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

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 /帖子 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

我缺少什么?。

2 个答案:

答案 0 :(得分:6)

你的问题就在这里:

root :to => "home#index"

在routes.rb文件中。

这告诉您的应用程序根网址(因此http://:3000 / URL)应该查找名为“home”且带有操作“index”的控制器。

为了实现这个目的,你需要在你的app / controllers文件夹中有一个HomeController.rb,在里面有一个def为'index'。

我建议运行此命令

rails generate controller home index

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

答案 1 :(得分:1)

@PallavSharma,让我提供一些更多信息来帮助您:

  

当您创建routes.rb时,您基本上会告诉Rails   domain.com/route将进入&#34;路线&#34; controller#action

您遇到的问题是您所声明的控制器(home)不存在。也不应该 - 它将是另一个控制器,在你的应用程序的其余部分几乎没有意义。如果您想要自定义root路径,我们通常只需ApplicationController这样:

#config/routes.rb
root to: "application#home"

这允许您设置以下内容:

#app/controllers/application_controller.rb
def home
    @posts = Post.index #-> whatever you want here
end

#app/views/application/home.html.erb
<!-- your code here -->

尽管使用posts#index作为索引可能是合乎逻辑的,甚至是最佳做法;这意味着您必须使用帖子index view作为您的家庭,以及posts/index。通过使用application#home,它可以为您提供更大的灵活性,尤其适用于大型应用