Search code examples
ruby-on-railsrubyroutessingle-table-inheritanceruby-on-rails-3

Broken Rails Routes after implementing Single Table Inheritance


I have implemented single table inheritance for a person class

class Person < ActiveRecord::Base

end


class Teacher < Person

end

class Student < Person

end

class Outsider < Person

end

And the create person seems to work creating Teacher, Student or Person according to the what is chosen in the form.select and the type attribute is added.

However, I seem to have broken the routes

<%= link_to 'Edit', edit_person_path(@deal) %> | <%= link_to 'Back', persons_path %>

They seem to point to teacher_path, student_path and outsider_path instead of person_path.

What changes need to be made in the routes?


Solution

  • first generate controllers for your models...

    rails generate controller Persons
    rails generate controller Teachers
    rails generate controller Students
    rails generate controller Outsiders
    

    then in routes.rb (rails 3)

    resources :persons
    resources :teachers
    resources :students
    resources :outsiders
    

    gives you REST routes

    e.g.

    persons GET    /persons(.:format) {:action=>"index", :controller=>"persons"}
    new_person GET    /person/new(.:format) {:action=>"new", :controller=>"persons"}
    edit_person GET    /persons/:id/edit(.:format) {:action=>"edit", :controller=>"persons"}
    person GET    /persons/:id(.:format) {:action=>"show", :controller=>"persons"} 
    persons POST   /spersons(.:format) {:action=>"create", :controller=>"persons"}    
    person PUT    /persons/:id(.:format) {:action=>"update", :controller=>"persons"}    
    person DELETE /persons/:id(.:format) {:action=>"destroy", :controller=>"persons"}
    

    the same for teacher, student and outsider

    check rake routes or rake routes | grep teachers