2014-10-30 50 views
1

我遵循Michael Hartl的rails教程(第5章,第5.3.4節)。我添加了所有的路線,但我無法按照他描述的那樣跟着他們。首先,我不確定是要手動輸入地址欄,還是通過點擊鏈接進行操作。它可以工作,如果我手動輸入他們,但鏈接沒有。其次,路由測試無法找到2個根路由。我試圖找到任何錯誤,但無濟於事。以下是我認爲與根路徑相關的部分。RailsTutorial:配置路由後出錯

的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 

我感到非常愚蠢的,我應該能想出解決辦法,但這對我來說都是如此新鮮。在此先感謝您的幫助。

+0

你可以去控制檯並輸入'rake routes'並讓我知道它返回什麼嗎? – 2014-10-30 17:16:26

+0

當你點擊這些鏈接/或手動輸入時,你會得到什麼? – 2014-10-30 17:17:47

回答

1

你只是鏈接到字符串'root_path''help_path',這是行不通的。這些是路徑助手,您需要調用的方法。只要改變這一點:

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

要這樣:

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

而且同樣無論你看到的是,它應該工作。

+1

Doh!我用* _path更新替換了前一個'#'中的#。那真是個愚蠢的錯誤。感謝您的快速回復! – PhotonJohn 2014-10-30 17:23:45