PhotonJohn
PhotonJohn

Reputation: 13

RailsTutorial: Error after configuring routes

I'm following Michael Hartl's rails tutorial (Chapter 5, section 5.3.4). I added all the routes but I cannot follow them as he describes. First, I am not sure if they are to be manually entered in the address bar or if they should work by clicking on the links. It works if I manually enter them but the links do not. Second, the routing test fails to find 2 root routes. I have tried to find any errors but to no avail. Here are the sections I think pertain to the root path.

routes.rb

Rails.application.routes.draw do
  root             'static_pages#home'
  get 'help'    => 'static_pages#help'
  get 'about'   => 'static_pages#about'
  get 'contact' => 'static_pages#contact' 

_header.html.erb

<header class="navbar navbar-fixed-top navbar-inverse">
  <div class="container">
    <%= link_to "sample app", 'root_path', id: "logo" %>
    <nav>
      <ul class="nav navbar-nav pull-right">
        <li><%= link_to "Home",   'root_path' %></li>
        <li><%= link_to "Help",   'help_path' %></li>
        <li><%= link_to "Log in", '#' %></li>
      </ul>
    </nav>
  </div>
</header>

site_layout_test.rb

class SiteLayoutTest < ActionDispatch::IntegrationTest
  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
  end
end

I feel pretty stupid as I should be able to figure this out but this is all so new to me. Thanks in advance for your help.

Upvotes: 1

Views: 153

Answers (1)

Nick Veys
Nick Veys

Reputation: 23939

You're just linking to the strings 'root_path' and 'help_path', which are not going to work. Those are path helpers, methods you need to call. Just change this:

<li><%= link_to "Home",   'root_path' %></li>

To this:

<li><%= link_to "Home", root_path %></li>

And similarly wherever you see that, and it should work.

Upvotes: 1

Related Questions