5

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?.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
PallavSharma
  • 439
  • 2
  • 12
  • 21

2 Answers2

8

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.

Dave Satch
  • 1,161
  • 6
  • 12
  • I did it now i got this message when i visit this 'home' button: Home#index Find me in app/views/home/index.html.erb Basically i want to show index page so that when clicked home button it shows all posts. – PallavSharma May 05 '14 at 06:10
  • Good. This means that the page you are seeing is the default Home Controller page. You have two options here. Option 1: I suggest following the tutorial here: http://guides.rubyonrails.org/v3.2.13/getting_started.html. You have now done all the steps up to 6.2 Adding a Link. Section 6.2 will show you how to link to a page with all the posts. – Dave Satch May 05 '14 at 06:17
  • 1
    Option 2: (Not recommended) If you want to take a shortcut, change your routes.rb file for the following: root :to => "posts#index" and the / URL will show you the posts controller – Dave Satch May 05 '14 at 06:17
  • thanks @dsatch i did the same and it worked, no need to create home controller. – PallavSharma May 05 '14 at 06:24
1

@PallavSharma, let me give you some more information to help you:

When you create routes.rb, you're basically telling Rails which domain.com/route is going to "route" to which controller#action

The problem you have is that your stated controller (home) does not exist. And nor should it - it will be another controller with almost no significance in the rest of your app. If you want a custom root path, we generally just the ApplicationController like this:

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

This allows you to set the following:

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

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

Although it might be logical, or even best practice, to use posts#index as the index; it means you'll have to use the posts index view for your home, as well as posts/index. By using application#home, it gives you a lot more flexibility, especially useful in large apps

Richard Peck
  • 76,116
  • 9
  • 93
  • 147