HiveBrain v1.2.0
Get Started
← Back to all entries
patternrubyrailsMinor

Controller for classrooms and students

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
classroomsstudentscontrollerforand

Problem

The actions I'm wondering about are members, add_student, and remove_student. They work just fine, but they aren't RESTful and it feels like they should be handled (perhaps) by the StudentsController.

Note that members and show are the same. The only difference is that the members.html.erb view iterates over and displays @classroom.students while show.html.erb displays info about the classroom itself.

class ClassroomsController  [])
  end

end


Here are the models:

# student.rb
class Student  :destroy, :inverse_of => :student
  has_many :classrooms, :through => :classroom_memberships
end

# classroom.rb
class Classroom  :classroom, :dependent => :destroy
  has_many :munchkins, :through => :classroom_memberships
end

Solution

The actions I'm wondering about are members, add_student, and remove_student. They work just fine, but they aren't RESTful [...]

True, they're not part the usual CRUD actions that can be expressed with HTTP verbs. But then neither are the default new or edit actions, so it's not like going beyond CRUD is automatically illegal.

Even so, associations do always present a bit of challenge. Are you adding the student to the class, or the class to the student?

However, in this case it appears that you already have a join model: ClassroomMembership. So going back to the CRUD actions, consider what you're creating when you call add_student: A membership.

So what you might want to do is simply make a ClassroomMembershipsController, since that's the resource you're manipulating. Said controller can then have the usual RESTful CRUD actions, keeping StudentsController or ClassroomController pretty clean.

This is just an idea, though. Again, if it's simpler to keep your actions on the ClassroomController then do that. But conceptually what you're doing is manipulating the association model.

Context

StackExchange Code Review Q#90986, answer score: 5

Revisions (0)

No revisions yet.