I have a form in which a student can enroll in a class. When a user submits a form, he is credited to the class and his billing information is saved. In other words, an Enrollment object is created, and the Student object is updated ... except that I cannot update Student . Is it possible? If so, how?
My models ...
class Student < ActiveRecord::Base has_many :enrollments end class Enrollment < ActiveRecord::Base belongs_to :student accepts_nested_attributes_for :student end
My (shortened) form ...
<%= form_for :enrollment, html: { id: "enrollment_form" } do |f| %> <%= f.fields_for :student_attributes do |student_builder| %> <%= student_builder.hidden_field :payment_name %> <% end %> <%= f.hidden_field :payment_token %> <div class="field terms"> <%= f.check_box :agreed_to_terms %> <%= f.label :agreed_to_terms, "I agree to the terms and conditions." %> </div> <% end %>
My controller ...
class EnrollmentsController < ApplicationController def create @enrollment = Enrollment.new(enrollment_params) @enrollment.clazz_id = @clazz.id @enrollment.student_id = @student.id @enrollment.save end private def enrollment_params params.require(:enrollment).permit(:payment_token, :agreed_to_terms, student_attributes: [:payment_name]) end end
POST options ...
{ "enrollment"=> { "student_attributes"=> { "payment_name"=> "MasterCard ending in 9840" }, "payment_token"=> "CC11ho86XxVqsUW7Cn9YjCHg?1376007969212", "agreed_to_terms"=> "1" }, "clazz_id"=> "7" }
I tried every permutation Student , students , _attributes Builder forms, but none of them does not work.
source share