为什么我的Home链接会尝试将我带到'/ root_path'而不是'/'?

时间:2012-11-25 15:16:50

标签: ruby-on-rails ruby ruby-on-rails-3.2

我正在完成本教程 - > http://ruby.railstutorial.org/chapters/filling-in-the-layout

您可以在此处遇到问题 - > intense-dusk-3202.herokuapp.com/

当我点击标题中的“主页”链接时,它会尝试转到http://intense-dusk-3202.herokuapp.com/root_path,而不仅仅是http://intense-dusk-3202.herokuapp.com/

这是我的routes.rb

SampleApp::Application.routes.draw do
  get "users/new"

  root :to => 'static_pages#home'

match '/signup', to: 'users#new'

  match '/help', to: 'static_pages#help'

  match '/about', to: 'static_pages#about'

  match '/contact', to: 'static_pages#contact'

end

这是我的app / views / layouts / _header.html.erb文件:

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

在你帮助我之前,你还需要看其他什么吗?

1 个答案:

答案 0 :(得分:4)

你不应该引用这些助手:

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

root_pathhelp_path和所有其他命名的路由助手实际上都是ruby方法。只需直接打电话给他们,不要在他们周围加上引号。