2017-08-23 6 views
1

私の結合テーブルcourse_studentsの更新に問題があります。Rails5 - has_manyでの結合テーブルの更新:through:fields_forの使用

私は新しい学生を作成するときに、私は(すでにコーステーブルに保存された2つのコースCourse1Course2から)ドロップダウンリストからコースを選択してcourse_studentsを更新したいです。私はcouse_studentsテーブルをこのようなものにしたい。

id | student_id | course_id |   created_at   |   updated_at   
----+------------+-----------+----------------------------+---------------------------- 

    1 |   1 |   1 | 2017-08-23 16:41:57.228094 | 2017-08-23 16:41:57.228094 

http://railscasts.com/episodes/17-habtm-checkboxes-revised?view=asciicastのおかげで、私は何とか次のコードは、radio_button_tagのために働くが分かりました。しかし、radio_button_tagselect_tagに変更するとエラーwrong number of argumentsが発生します。

また、情報が少し古いと思われるため、これが正しい方法かどうかはわかりません。いくつかのGoogle検索の後、私は多くの場合、人々がnew.html.erbfield_forを使用していることが分かったので、私は試しましたが、course_studentsテーブルを更新できませんでした。

将来、teacher_data_from、class_roomなどの他の列をcourse_studentsテーブルに追加したいと思います。

本当にありがとうございます。

・モデル

class Student < ApplicationRecord 
    has_many :course_students 
    has_many :courses, :through => :course_students 

    accepts_nested_attributes_for :course_students 
end 

class Course < ApplicationRecord 
    has_many :course_students 
    has_many :students, :through => :course_students 

end 

class CourseStudent < ApplicationRecord 
    belongs_to :student 
    belongs_to :course 

end 

・マイグレーション

class CreateStudents < ActiveRecord::Migration[5.1] 
    def change 
    create_table :students do |t| 
     t.string :first_name 
     t.string :middle_name 
     t.string :last_name 

     t.timestamps 
    end 
    end 
end 

class CreateCourses < ActiveRecord::Migration[5.1] 
    def change 
    create_table :courses do |t| 
     t.string :name 

     t.timestamps 
    end 
    end 
end 

class CreateCourseStudents < ActiveRecord::Migration[5.1] 
    def change 
    create_table :course_students do |t| 
     t.integer :student_id 
     t.integer :course_id 

     t.timestamps 
    end 
    end 
end 

・コーステーブル

id |  name  |   created_at   |   updated_at   
----+--------------+----------------------------+---------------------------- 
1 | Course1 | 2017-08-22 20:03:46.226893 | 2017-08-22 20:03:46.226893 
2 | Course2 | 2017-08-22 20:03:46.228765 | 2017-08-22 20:03:46.228765 

・student.controller、強いのparams。

def student_params 
    params.require(:student).permit(:first_name, :middle_name, :last_name, course_ids: [], date_froms: []) 
    end 

・new.html.erb

<h1>Create new student</h1> 
<%= form_for(@student) do |f| %> 
    <%= f.label :first_name %> 
    <%= f.text_field :first_name %> 

    <%= f.label :middle_name %> 
    <%= f.text_field :middle_name %> 

    <%= f.label :last_name %> 
    <%= f.text_field :last_name %> 

    <%= Course.all.each do |course| %> 
     <%= radio_button_tag "student[course_ids][]", course.id, @student.course_ids.include?(course.id), id: dom_id(course)%> 
     <%= label_tag dom_id(course), course.name %> 
    <% end %> 

    <%= f.submit "Create new student", class: "btn btn-primary" %> 
<% end %> 

答えて

1

利用nested_form_forの代わりに、form_forとcourse_studentsはフォームヘルパーが提供するネストされたfield_forを使用するため。 https://github.com/ryanb/nested_form

<h1>Create new student</h1> 

<%= nested_form_for(@student) do |f| %> 
    <%= f.label :first_name %> 
    <%= f.text_field :first_name %> 

    <%= f.label :middle_name %> 
    <%= f.text_field :middle_name %> 

    <%= f.label :last_name %> 
    <%= f.text_field :last_name %> 

    <%= f.fields_for :course_students do |ff| %> 
    <%= ff.hidden_field :student_id, value: @student.id %> 
    <%= ff.collection_select :course_id, Course.all, :id, :name %> 
    <% end %> 

    <%= f.submit "Create new student", class: "btn btn-primary" %> 
<% end %> 

あなたは、このようなコントローラでcourse_students paramsはを許可する必要があります。

def student_params 
    params.require(:student).permit(:first_name, :middle_name, :last_name, course_students_attributes: [ :course_id, :student_id]) 
end 
+0

ありがとうございました。あなたのコメントは私に手がかりを与えました!コードを投稿しました。 –

0

は、次のコードは、働いていたことが判明!

students_controller

def new 
    @student = Student.new 
    @student.course_students.build 
    end 

    private 

    def student_params 
    params.require(:student).permit(:first_name, :middle_name, :last_name, course_students_attributes: [ :course_id, :student_id]) 
    end 

new.html.erb

<%= f.fields_for :course_students do |ff| %> 
    <%= ff.hidden_field :student_id, value: @student.id %> 
    <%= ff.collection_select :course_id, Course.all, :id, :name %> 
<% end %> 

ありがとう!

関連する問題