2014-03-19 293 views
0

所以,我有以下代碼。我想訪問timespans_path, 但我不能。Rails無法找到路徑

<% content_for :div_header do%> 
    <h1> Welcome, <%= @l_user.name %> </h1> 
<% end %> 

<% content_for :div_sub_header do %> 
    <ul> 
    <li><%= link_to "show entries", entries_path %></li> 
    <li><%= link_to "show groups", groups_path %> 
    <% if can? :read, Subgroup %> 
     , 
     <%= link_to " subgroups", subgroups_path %> 
     </li> 
    <% end %> 
    <li><%= link_to "show users", users_path %></li> 
    <li><%= link_to "show actioncodes", actioncodes_path %></li> 
    <li><%= link_to "show timespans", timespans_path %></li> 
    </ul> 
<% end %> 

我總是得到這些錯誤:

NameError in Application#welcome 
Showing C:/xampp/htdocs/fluxcapacitor/app/views/application/welcome.html.erb where line #16 raised: 
undefined local variable or method `timespans_path' for #<#<Class:0x58b8610>:0x58b7e18> 

這是我route.rb

Fluxcapacitor::Application.routes.draw do 
    root 'application#welcome' 

    get 'login' => 'application#login' 
    post 'login' => 'application#process_login' 

    post '' => 'application#process_login' 

    post 'send_request_account_mail' => 'application#send_request_account_mail' 
    post 'send_forgot_password_mail' => 'application#send_forgot_password_mail' 

    get 'forgot_password' => 'application#forgot_password' 
    get 'request_account' => 'application#request_account' 

    get 'welcome' => 'application#welcome' 
    get 'logout' => 'application#logout' 

    if Rails.env.development? 
    get 'display_mail' => 'application#display_mail' 
    end 

    resources :users 

    get 'multiple_new' => 'users#multiple_new' 
    post 'multiple_new' => 'users#multiple_new' 
    post 'multiple_create' => 'users#multiple_create' 

    get 'users/:id/:hash/cal' => 'users#cal' 

    resources :actioncodes 

    resources :entries 

    resources :timespans 

    resources :groups do 
    member do 
     get 'search_admin' 
     post 'search_admin' 
     post 'add_admin' 
     get 'remove_admin' 
     post 'remove_admin' 
    end 
    end 

    resources :subgroups do 
    member do 
     get 'search_user' 
     post 'search_user' 
     post 'add_user' 
     get 'remove_user' 
     post 'remove_user' 

     get 'remove_admin' 
     post 'remove_admin' 
    end 
    end 
end 

爲什麼會出現錯誤?我該如何解決它?

+0

你也應該放在你'route.rb' –

+0

份額的問題你的路由文件以及 – Abk

+1

你必須增加資源:時間跨度在你的路由文件,否則路線助手不會明白timespans_path – Abk

回答

1

在route.rb添加

resources :timespans 

0

看起來你還沒有定義了一個名爲timespans路徑,這樣的觀點不知道什麼渲染URL,並引發錯誤。

如果你有一個名爲Timespan模型,然後添加resources :timespans到你的路由文件將創建一個名爲timespans_path(等等)路徑,指向/timespans

您還可以創建稱爲使用:as選項timespans路徑任意路徑,例如:如果你有在未來的路徑問題

get "/the_url", to: "the_controller#the_action", as: :timespans 

,請注意,您可以使用耙子任務rake routes列出所有您生成的路由和路徑助手的名稱,這對調試非常有幫助。