Insert and update using strong parameters for nested own_to association

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.

+4
source share
1 answer

Ok, I see a few things:

  • The nested attributes described in the API "allow you to save the attributes of related records through the parent ." It means that

     class Enrollment < ActiveRecord::Base belongs_to :student accepts_nested_attributes_for :student end 

    essentially does not work because you are trying to accept nested attributes from a parent. So you need to start by rethinking your Active Record data.

    If we pretend that everything is squared, then there are more syntax errors:

  • Replace

     <%= f.fields_for :student_attributes do |student_builder| %> 

    from

     <%= f.fields_for :students do |student_builder| %> 

    This may be confusing, but passing :students to the fields_for calls a nested student object, whereas :student_attributes is the hash key from the POST parameters generated by fields_for .

  • In your strong settings, you also need to allow the student :id , so that your update action has a link. Otherwise, he will simply create a new student. Therefore change it to

     private def enrollment_params params.require(:enrollment).permit(:payment_token, :agreed_to_terms, student_attributes: [:payment_name, :id]) end 

I'm not sure that's all, but hopefully this is the beginning. Good luck.

+5
source

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


All Articles