ActiveRecord
You'll probably be looking for the <<
ActiveRecord feature:
#config/routes.rb
resources :students do
#could be a member route
match :add, via: [:get, :post]
end
#app/controller/students_controller.rb
def add
@student = Student.find(params[:student_id])
if request.post?
@course = Course.find(params[:course_id])
@student.student_course_assignments << @course
end
end
#app/views/students/add.html.erb
<%= form_for @student, method: :post do |f| %>
<%= f.text_field :course_id %>
<% end %>
Yours
For your code, I'd do this:
<%= form_for @student do |f| %>
<% Courses.all.each do |course| %>
<%= f.checkbox :student_course_assignment_ids, course.id %>
<span><%= course.name %></span>
<% end %>
<% end %>
This will populate the collection of :student_course_assignments for you I think. You shouldn't have to use accepts_nested_attributes_for
if you're not creating new course
objects