2016-11-26 104 views
0

我的代碼_header.html.erb:沒有路由匹配GET root_path

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

的 「root_path」 上面應該被評估爲 「/」,但事實並非如此。我現在用的邁克爾·哈特爾教程

的routes.rb

Rails.application.routes.draw do 
    root 'static_pages#home' 
    get '/help', to: 'static_pages#help' 
    get '/about', to: 'static_pages#about' 
    get '/contact', to: 'static_pages#contact' 
    get '/signup', to: 'users#new' 
    post '/signup', to: 'users#create' 
    resources :users 
end 

耙路線:

Richard:sample_app richard$ rake routes 
    Prefix Verb URI Pattern    Controller#Action 
    root GET /      static_pages#home 
    help GET /help(.:format)   static_pages#help 
    about GET /about(.:format)   static_pages#about 
    contact GET /contact(.:format)  static_pages#contact 
    signup GET /signup(.:format)   users#new 
      POST /signup(.:format)   users#create 
    users GET /users(.:format)   users#index 
      POST /users(.:format)   users#create 
new_user GET /users/new(.:format)  users#new 
edit_user GET /users/:id/edit(.:format) users#edit 
    user GET /users/:id(.:format)  users#show 
      PATCH /users/:id(.:format)  users#update 
      PUT /users/:id(.:format)  users#update 
      DELETE /users/:id(.:format)  users#destroy 

回答

0

我發現了這個問題。在「/views/layout/_header.html.erb」和_footer.html.erb中,我在「root_path」和「about_path」中使用了引號,而不僅僅是變量。檢查所有類似的變量以確保不引用它們

0

你的根是http://<host>/而不是http://<host>/root_path下。 root_path是返回url而不是路徑本身的方法的名稱。

+0

問題是我使用「root_path」作爲應該評估的變量爲「/」,但它不 – Richard

+0

請發表您的視圖 –

1

您不應將root_path包含在報價中,因爲它是一種路線幫助程序方法,其計算結果爲/。只是這樣做:

<%= link_to "sample app", root_path, id: "logo" %> 

,或者如果你喜歡用引號然後用"#{root_path}"

相關問題