![](/img/trans.png)
[英]Rails form_for NoMethodError - can't find where broken path is
[英]Rails can't find 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
为什么会出现错误? 我该如何解决?
加
resources :timespans
在你的route.rb
似乎您尚未定义一个名为timespans
的路径,因此视图不知道要呈现的URL,并引发了错误。
如果您有一个名为Timespan
的模型,则向您的路由文件添加resources :timespans
将创建一个名为timespans_path
的路径(以及其他路径),指向/timespans
。
您也可以使用:as
选项创建称为timespans
path的任意路径,例如:
get "/the_url", to: "the_controller#the_action", as: :timespans
如果将来在路径方面遇到问题,请注意,可以使用rake任务rake routes
列出所有生成的路由及其路径助手的名称,这对于调试非常有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.