The key to solving this is to correctly identify the resource you are modifying. In this case, the resource you are modifying is the relationship between the class and the student, which I will name Enrollment.
In Rails, it has become customary to use has_many :throughpreferably has_and_belongs_to_many. You might want to change your domain logic to match custom, but you can also win the trend if you are really sure that metadata should not be stored in relation to relationships.
One of the key ideas for REST is that RESTful resources do not need to map models. You should create EnrollmentsControllerand add a line in config / routes.rb:
map.resources :enrollments
Then you can create and delete your relations like this:
class EnrollmentsController < ApplicationController
def create
@student = Student.find(params[:student_id])
@course = Course.find(params[:course_id])
@student.courses << @course
if @student.save
else
end
end
def destroy
@student = Student.find(params[:student_id])
@course = @student.courses.find(params[:course_id])
@student.courses.delete( @course )
end
end
:
<%= button_to "Enroll", enrollments_path(:student_id => current_student.id, :course_id => @course.id ), :method => :post %>
<%= button_to "Withdraw", enrollment_path(1, :student_id => current_student.id, :course_id => @course.id ), :method => :delete %>
1 , :enrollment_id , , Rails.