2016-08-24 85 views
0

我試圖將導航欄上的thredded/forum路由到鏈接,錯誤阻止了我。不知道它是一個安裝問題還是路徑問題。謝謝!沒有路線匹配{:action =>「show」,:controller =>「forum」}}缺少必需的鍵:[:id]

的routes.rb

Rails.application.routes.draw do 
    resources :links 
    mount Thredded::Engine => '/forum' # creates about_path 
    resources :forum 
    devise_for :users 
    root "pages#home" 
    get "about" => "pages#about" 
end 

主頁/ _header.html.erb

<nav class="navbar navbar-static-top navbar-default" role="navigation"> 
    <div class="container"> 
    <!-- Brand and toggle get grouped for better mobile display --> 
    <div class="navbar-header"> 
     <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false"> 
     <span class="sr-only">Toggle navigation</span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     <span class="icon-bar"></span> 
     </button> 
     <%= link_to "Rowlund", root_path, class: "navbar-brand" %> 
     <nav class="navbar navbar-default navbar-fixed-bottom"> 
     <div class="container"> 
     </div> 
     </nav> 
    </div> 

    <!-- Collect the nav links, forms, and other content for toggling --> 

      </li> 
     </ul> 
     <ul class="nav navbar-nav navbar-right"> 
      <li><%= link_to "Home", root_path %></li> 
      <li><%= link_to "Forum", forum_path%></li> 
      <li><%= link_to "About", about_path %></li> 
      <% if user_signed_in? %> 
      <li><%= link_to "Account Settings", edit_user_registration_path %> </li> 
      <li><%= link_to "Log out", destroy_user_session_path, method:  :delete %></li> 
      <% else %> 
      <li><%= link_to "Log in", new_user_session_path %></li> 
      <% end %> 
     </ul> 
     </li> 
     </ul> 
    </div><!-- /.navbar-collapse --> 
    </div><!-- /.container --> 
</nav> 

回答

2

如果運行rake routesrake routes | grep forum,你會發現在名單如下路線路線。

forum GET /forums/:id(.:format)   forums#show 

路線/forums/:id映射到forums#show,你需要在id通過在URL中。對於如:

forums_path(3) => /forums/3 
forums_path(Forum.find(1)) => /forums/1 

在你的代碼,你不傳遞idforum_path。那就是爲什麼你得到錯誤missing required keys: [:id]

因爲你沒有一個特定的forum對象映射在您的導航欄鏈接中,您應該更好地將鏈接映射到index操作。更改以下鏈接

<li><%= link_to "Forum", forum_path %></li> 

<li><%= link_to "Forum", forums_path %></li> 

注意forums_path映射到forums#index

相關問題