【发布时间】:2016-09-27 14:56:36
【问题描述】:
我有一个创建老师的表格,在这个表格中有一个表格,我在其中注册了他们可以教授的科目以及一些相关的经验。
但我不知道如何在模型中、控制器中和视图中执行此操作,因为已将嵌套字段添加到我的教师表单中,并在模型中使用nested_attributes,并在控制器中允许它。但我不知道该怎么做..
型号
class Tutor < User
has_many :tutor_subjects
accepts_nested_attributes_for :tutor_subjects
end
在控制器中我有功能......
def create
@tutor = Tutor.new(tutor_params)
@tutor.save
end
def tutor_params
params.require(:tutor).permit(:email,
:first_name,
:last_name,
:gender,
:password,
tutor_subjects_attributes: [])
end
end
还有风景
= simple_form_for [:admin, @tutor] do |f|
= f.error_notification
.col-md-8.col-sm-12
.form-inputs
= f.input :email
= f.input :password
= f.input :first_name
= f.input :last_name
= f.input :gender, collection: {Male: 1, Female: 2}, include_blank: "--Select gender--"
%h2.well.well-sm Subjects
%table.table
%thead
%tr
%th Subject
%th Experience
%th Options
%tbody#subjects
= f.simple_fields_for :tutor_subjects_attributes do |s|
%tr.subject0
%td
= s.input :subject_id, collection: Subject.all, label: false, include_blank: "--Select subject--", input_html: { id: "tutor_subjects_attributes_0_subject_id", name: 'tutor[tutor_subjects_attributes][0][subject_id]', class: "subject" }
%td
= s.input :experience, as: :text, label: false, input_html: { placeholder: "Type your experience", name: 'tutor[tutor_subjects_attributes][0][experience]', id: "tutor_subjects_attributes_0_experience" }
%td.options
= link_to :add_subject, "#", id: "add_subject", class: "btn btn-small btn-info"
【问题讨论】:
-
那么问题出在哪里?是
tutor_subjects_attributes的强参数吗? -
问题是我想将参数保存到嵌套模型中,发送很多行,但不起作用
-
在下面尝试过我的答案吗?
标签: ruby-on-rails ruby simple-form nested-attributes ruby-on-rails-5