Rails找不到Path

时间:2014-03-19 07:51:57

标签: ruby-on-rails path

所以,我有以下代码。我想访问timespans_path, 但我不能。

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

为什么我收到错误?我该如何解决?

2 个答案:

答案 0 :(得分:1)

添加

resources :timespans
你的route.rb

中的

答案 1 :(得分:0)

您似乎还没有定义名为timespans的路径,因此该视图无法知道要渲染的网址并引发错误。

如果您有一个名为Timespan的模型,那么将resources :timespans添加到您的路线文件中将创建一个名为timespans_path的路径(以及其他),指向/timespans

您还可以使用timespans选项创建名为:as路径的任意路径,例如:

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

如果将来遇到路径问题,请注意您可以使用rake任务rake routes列出所有生成的路由及其路径助手的名称,这对调试非常有帮助。