Rails - HABTM 查找或创建嵌套表单
Rails - HABTM find or create nested form
我找到了 this。
我现在的问题是如何创建表单,我正在尝试:
#students.row
= f.fields_for :classrooms_students, f.object.classrooms_students do |cs|
= cs.fields_for :student, cs.object.student do |s|
/ .record is used to fill fields when teacher clicks a .result
.record
.col-sm-6 style=("margin-top: 10px;")
= s.label :name
= s.text_field :name, class: 'form-control autocomplete', data: {autocomplete: :name}
.results
.col-sm-6 style=("margin-top: 10px;")
= s.label :email
= s.text_field :email, class: 'form-control autocomplete', data: {autocomplete: :email}
.results
.col-sm-12 style=("margin-top: 10px;")
= cs.link_to_remove 'Remove Student', class: 'btn btn-danger'
.col-sm-12 style=("margin-top: 10px;")
= f.link_to_add 'Add Student', :classrooms_students, "data-target" => '#students', class: 'btn btn-primary'
问题是在服务器中我得到:
{"classroom"=>
{"name"=>"qwerqqeq",
"description"=>"",
"note"=>"",
"classrooms_students_attributes"=>
{"0"=>
{"student_attributes"=>{"name"=>"vsrjh", "email"=>"fgh@fgh.com", "id"=>"33"},
"_destroy"=>"false",
"id"=>""},
"1"=>
{"student_attributes"=>{"name"=>"cvbcbc", "email"=>"yui@yui.com", "id"=>"32"},
"_destroy"=>"false",
"id"=>""}}}}
连接哈希上的 id
是 ''
Rails 将如何查找记录?
所以我得到:
undefined method `to_sym' for nil:NilClass.
我应该添加一个 id
列来加入 table 吗?
我添加了一个index:
add_column :classrooms_students, :id, :primary_key
我找到了 this。
我现在的问题是如何创建表单,我正在尝试:
#students.row
= f.fields_for :classrooms_students, f.object.classrooms_students do |cs|
= cs.fields_for :student, cs.object.student do |s|
/ .record is used to fill fields when teacher clicks a .result
.record
.col-sm-6 style=("margin-top: 10px;")
= s.label :name
= s.text_field :name, class: 'form-control autocomplete', data: {autocomplete: :name}
.results
.col-sm-6 style=("margin-top: 10px;")
= s.label :email
= s.text_field :email, class: 'form-control autocomplete', data: {autocomplete: :email}
.results
.col-sm-12 style=("margin-top: 10px;")
= cs.link_to_remove 'Remove Student', class: 'btn btn-danger'
.col-sm-12 style=("margin-top: 10px;")
= f.link_to_add 'Add Student', :classrooms_students, "data-target" => '#students', class: 'btn btn-primary'
问题是在服务器中我得到:
{"classroom"=>
{"name"=>"qwerqqeq",
"description"=>"",
"note"=>"",
"classrooms_students_attributes"=>
{"0"=>
{"student_attributes"=>{"name"=>"vsrjh", "email"=>"fgh@fgh.com", "id"=>"33"},
"_destroy"=>"false",
"id"=>""},
"1"=>
{"student_attributes"=>{"name"=>"cvbcbc", "email"=>"yui@yui.com", "id"=>"32"},
"_destroy"=>"false",
"id"=>""}}}}
连接哈希上的 id
是 ''
Rails 将如何查找记录?
所以我得到:
undefined method `to_sym' for nil:NilClass.
我应该添加一个 id
列来加入 table 吗?
我添加了一个index:
add_column :classrooms_students, :id, :primary_key