0

我有一個表單來創建一個老師,並且在這個表單中有一個表格,我可以在其中註冊他們可以教授的主題以及一些相關的經驗。rails form從同一個實例發送很多行

但是我不知道該怎麼做,在模型中,在控制器和視圖中,因爲有添加嵌套字段到我的老師窗體,並在模型中使用nested_attributes,並允許它在控制器中。但我不知道該怎麼做..

型號

class Tutor < User 
    has_many :tutor_subjects 
    accepts_nested_attributes_for :tutor_subjects 
end 

在控制器I具備的功能...

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" 
+0

所以有什麼問題?它與'tutor_subjects_attributes'強參數? – Aleksey

+0

問題是,我想保存參數到嵌套模型,發送很多行,但不工作 – jacr1614

+0

確實在我的答案下面? – Aleksey

回答

0

我不知道我是否理解你的問題,但好像你有問題處理嵌套屬性的強參數。

tutor_params應該像

def tutor_params 
    params.require(:tutor).permit(
    :email, 
    ... 
    tutor_subjects_attributes: [ 
     :subject_id, 
     :experience 
    ] 
) 
    end 
end 
相關問題