简体   繁体   English

Rails 3 路线错误 controller

[英]Rails 3 routes to wrong controller

I wanted to create a new action and I call it "showemployees".我想创建一个新动作,我称之为“showemployees”。 That's what I did already:这就是我已经做的:

-> in the controller: -> 在 controller 中:

def showemployees
end

-> creating app/views/employees/showemployees.html.erb -> 创建应用程序/视图/员工/showemployees.html.erb

-> in config/routes -> 在配置/路由中

match "/employees/showemployees" => "employees#showemployees"

I thought this is enough for opening the showemployees.html.erb now via localhost:3000/employees/showemployees , but it seems like Rails still routes through the show action (from resources:employees) and doesn't take the showemployees-action, because it tells me我认为这足以打开 showemployees.html.erb 现在通过localhost:3000/employees/showemployees ,但似乎 Rails 仍然通过 show 操作(来自资源:employees)并且不采取 showemployees-action,因为它告诉我

ActiveRecord::RecordNotFound in EmployeesController#show
Couldn't find Employee with ID=showemployees

What do I need to change so Rails takes the showemployees-action?我需要更改什么才能让 Rails 采取 showemployees-action?


the source code of my route:我的路线的源代码:

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

try to walk by Rails-way, if you want to get collection, use the collection尝试走 Rails-way,如果你想收集,使用收集

resources :employees do
  collection do
    get :showemployees
  end
end

If you post your full routes file we can make a definitive call, but based on the error message, it looks like you have a broader route definition mapping to employees#show defined above this route in such a way that it is getting matched.如果您发布完整的路由文件,我们可以进行明确的调用,但根据错误消息,看起来您有更广泛的路由定义映射到在此路由上方定义的 employees#show 以使其匹配的方式。

Routes are evaluated in the order they are defined, so if you have a very broad route pattern defined above a narrow one, your narrow route will never be called.路由是按照定义的顺序进行评估的,所以如果你在窄路由之上定义了一个非常宽泛的路由模式,你的窄路由将永远不会被调用。

edit: you'll want to take the forward slash out of your route and add the showemployees to the actual URL, so that it reads编辑:您需要将正斜杠从您的路线中删除,并将 showemployees 添加到实际的 URL 中,以便它读取

 match "employees/showemployees" => "employees#showemployees" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM