我有一个表格,学生可以在其中注册课程。当用户提交表单时,他就注册了课程并保存了他的付款信息。换句话说,Enrollment
创建了一个对象并Student
更新了该对象......除了我无法Student
更新。这可能吗?如果是这样,怎么做?
我的模型...
class Student < ActiveRecord::Base
has_many :enrollments
end
class Enrollment < ActiveRecord::Base
belongs_to :student
accepts_nested_attributes_for :student
end
我的(缩写)表格...
<%= 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 %>
我的控制器...
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参数...
{
"enrollment"=> {
"student_attributes"=> {
"payment_name"=> "MasterCard ending in 9840"
},
"payment_token"=> "CC11ho86XxVqsUW7Cn9YjCHg?1376007969212",
"agreed_to_terms"=> "1"
},
"clazz_id"=> "7"
}
我已经在表单生成器中尝试了,的所有排列student
,但它们似乎都不起作用。students
_attributes