2014-05-05 62 views
5

进出口新的使用基础,我一直在使用支架 创建简单的帖子应用程序,我已经做了以下步骤:路由错误“未初始化常量的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 

现在应用运行良好@本地主机p ORT 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 

这是什么im缺少?

+1

您是否拥有HomeController? – Iceman

+0

@iceman不应该在那里定义什么? – PallavSharma

+1

你正在将你的'/'url生根到HomeController。运行'rails g controller home index',然后重试。 – Iceman

回答

6

你的问题是,这条线的位置:

root :to => "home#index" 

在routes.rb中文件。

这告诉你的应用程序根URL(所以HTTP://:3000/URL)应该寻找一个叫“家”有动作“索引”控制器。

对于这个工作,你需要在你的应用程序/控制器文件夹,里面对于“索引”一DEF一个HomeController.rb。

我建议在运行此命令

rails generate controller home index 

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

+0

我做了它现在我得到这个消息,当我访问这个'家'按钮:首页#索引 找到我的应用程序/ views/home/index.html.erb 基本上我想显示索引页,以便当点击主页按钮它显示所有帖子。 – PallavSharma

+0

好。这意味着您看到的页面是默认的Home Controller页面。你有两个选择。选项1:我建议遵循这里的教程:http://guides.rubyonrails.org/v3.2.13/getting_started.html。您现在已经完成了所有步骤,最多可以添加一个链接。第6.2节将向您展示如何链接到包含所有帖子的页面。 –

+1

选项2 :(不推荐)如果你想采取一个快捷方式,改变你的routes.rb文件为以下内容:root:to =>“posts#index”和/ URL将显示你的帖子控制器 –

1

@PallavSharma,让我给你一些更多的信息,以帮助您:

当您创建routes.rb,基本上就这 domain.com/route是要“路线”的Rails到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,它为您提供了更多的灵活性,特别适用于大型应用程序

+1

感谢您与我分享这些有用的信息。 :) – PallavSharma

+1

没问题,希望它有帮助 –

相关问题