Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Routing Error in rails app 'uninitialized constant HomeController'

Im new to use foundation and i have created simple Posts app using scaffold and i have done following steps:

rails new blog

then added following to Gemfile

gem 'foundation-rails'
group :development do
  gem 'rails_layout'
end

and then

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

Now app runs fine @ local host port 3000/posts enter image description here But when i click on 'Home' button in nav-bar it generates errors: enter image description here

application.html.erb file:

<!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 file :

<%# 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>

My routes.rb file:

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

What is it that im missing?.

like image 757
PallavSharma Avatar asked May 05 '14 05:05

PallavSharma


1 Answers

Your issue is this line here:

root :to => "home#index"

In the routes.rb file.

This tells your application that the root URL (so the http://:3000/ URL) should look for a controller called 'home' with an action 'index'.

For this to work you would need to have a HomeController.rb in your app/controllers folder and inside that a def for 'index'.

I recommend running this command

rails generate controller home index

To generate the home controller. Many tutorials will give you this line to run BEFORE you run the scaffold command.

like image 75
Dave Satch Avatar answered Sep 22 '22 23:09

Dave Satch