我想创建一个新动作,我称之为“showemployees”。这就是我已经做过的事情:
- >在控制器中:
def showemployees
end
- >创建app / views / employees / showemployees.html.erb
- >在config / routes
中 match "/employees/showemployees" => "employees#showemployees"
我认为这足以通过 localhost:3000 / employees / showemployees 打开showemployees.html.erb,但似乎Rails仍然通过show action(来自资源:employees)进行路由而且不接受showemployees-action,因为它告诉我
ActiveRecord::RecordNotFound in EmployeesController#show
Couldn't find Employee with ID=showemployees
我需要更改什么才能让Rails接受showemployees-action?
我路线的源代码:
System::Application.routes.draw do
match "/employees/showemployees" => "employees#showemployees" #für showemployees.html.erb
root :to => "employees#index"
resources :course_to_dos
resources :current_qualifications
resources :expected_qualifications
resources :skills
resources :employees
resources :positions
resources :admin
end
答案 0 :(得分:5)
尝试按照Rails方式行走,如果你想获得收藏,请使用该集合
resources :employees do
collection do
get :showemployees
end
end
答案 1 :(得分:3)
如果您发布完整路由文件,我们可以进行最终调用,但根据错误消息,您看起来有一个更广泛的路由定义映射到员工#show在此路由上方定义,以便它获得匹配。
路线按照定义的顺序进行评估,因此如果您在狭窄的路线上定义了非常宽的路线模式,则永远不会调用您的窄路线。
编辑:你需要从你的路线中取出正斜杠并将showemployees添加到实际的URL中,以便它显示
match "employees/showemployees" => "employees#showemployees"