2014-05-05 91 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

沒問題,希望它有幫助 –

相關問題