没有路由匹配{:action =>“show”,:controller =>“forum”}缺少必需的密钥:[:id]

时间:2016-08-24 03:30:21

标签: ruby-on-rails

我正在尝试将thredded /论坛路由到导航栏上的链接,但是这个错误阻碍了我。不确定它是安装问题还是路径问题。谢谢!

的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>

1 个答案:

答案 0 :(得分:2)

如果您运行rake routesrake routes | grep forum,您应该在路线列表中找到以下路线。

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

路线/forums/:id映射到forums#show,您需要在网址中传入id。例如:

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

在您的代码中,您没有将id传递给forum_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