简体   繁体   中英

Rails 3 routes to wrong controller

I wanted to create a new action and I call it "showemployees". That's what I did already:

-> in the controller:

def showemployees
end

-> creating app/views/employees/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

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

What do I need to change so Rails takes the 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

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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