Rails RESTful to delete in nested resources

Ok, so here is an example script. There is a student resource resources :students, and students have and belong to many collections: resources :clubs, resources :majorsetc.

This way we can easily customize our routes ...

resources :clubs do
  resources :students
end
resources :majors do
  resources :students
end
resources :students do
  resources :clubs
  resources :majors
end

which generates us a bunch of standard RESTful routes

  • / clubs
  • / clubs /: identifier
  • / clubs /: club_id / students
  • / clubs /: club_id / students /: id of majors
  • / majors /: identifier
  • / majors /: major_id / students
  • / majors /: major_id / students /: identifier
  • / students
  • / students /: identifier
  • / students /: student_id / clubs
  • / students /: student_id / clubs /: identifier
  • / students /: student_id / majors
  • / students /: student_id / majors /: identifier

, . REST, ? /majors/:major_id/students/:id "". id: StudentsController#destroy, . ! , , DELETE /students/:student_id/majors/:id, UnderwaterBasketweaving ... !

destroyController, MajorsController StudentsController club_id major_id student_id, , "" "GraduatingClasses" .. , , ... , . , , ... "" ...

? , make_resourceful resource_controller, UnderwaterBasketweaving Joe Majors JohnDoe UnderwaterBasketweaving. , , "".

, ? UnderwaterBasketweaving → Joe but UnderwaterBasketweaving + Joe , , , Joe, UnderwaterBasketweaving, , ? , , , (MVC RV... "" , , ). , ; ...

, , ...?

... major.student.delete... "" ActiveRecord , "" , "" AR?

+3
1

, RESTful aproach has_many :through . , .

resources :majors do
  resources :studies
  resources :students
end
resources :students do
  resources :studies
  resources :majors
end

, :

class Major < ActiverRecord::Base
  has_many :studies
  has_many :students, :through => :studies
end

... (, , )

@student.major, @student.studies.where :major => @major.

+2

Source: https://habr.com/ru/post/1773503/


All Articles