0

私は先生を作成するフォームを持っています。このフォームには、教えることができる科目を登録するテーブルと、それに関連する経験があります。railsフォームは同じインスタンスから多くの行を送信します

しかし、私は先生のフォームにネストされたフィールドを追加し、モデルでnested_attributesを使用してコントローラでそれを許可しているので、モデル、コントローラ、およびビューでそれを行う方法はわかりません。しかし、私は私が

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

...私は機能を持っている...それを行う方法コントローラ内の

モデル

class Tutor < User 
    has_many :tutor_subjects 
    accepts_nested_attributes_for :tutor_subjects 
end 

を知りませんあなたの質問を正しく理解しているかどうかはわかりませんが、ネストされた属性の強いパラメータを扱う際に問題があるようです。

あなたのような問題は何tutor_params

def tutor_params 
    params.require(:tutor).permit(
    :email, 
    ... 
    tutor_subjects_attributes: [ 
     :subject_id, 
     :experience 
    ] 
) 
    end 
end 
関連する問題